2013-04-19 7 views
7

Sto provando a testare la lunghezza di un attributo del codice postale per assicurarne la lunghezza di 5 caratteri. In questo momento sto testando per assicurarmi che non sia vuoto e quindi troppo corto con 4 caratteri e troppo lungo con 6 caratteri.Esiste un test rspec per la lunghezza esatta di un attributo?

C'è un modo per testarlo con esattamente 5 caratteri? Finora non ho trovato nulla online o nel libro rspec.

risposta

2

Se si sta testando una convalida su un modello ActiveRecord, si consiglia di provare shoulda-matchers. Fornisce un sacco di utili estensioni RSpec utili per Rails. Si potrebbe scrivere un semplice spec una riga per l'attributo codice di avviamento postale:

describe Address do 
    it { should ensure_length_of(:zip_code).is_equal_to(5).with_message(/invalid/) } 
end 
11

RSpec permette questo:

expect("this string").to have(5).characters 

si può effettivamente scrivere nulla, invece di 'personaggi', è solo zucchero sintattico. Tutto ciò che sta accadendo è che RSpec sta chiamando #length sull'argomento.

Tuttavia, dalla tua domanda suona come si vuole realmente testare la convalida, nel qual caso avrei Seguire @ consiglio di rossta.

UPDATE:

Dal RSpec 3, questo è non fa più parte del RSpec-aspettative, ma è disponibile come una gemma separata: https://github.com/rspec/rspec-collection_matchers

9

non c'è più have matcher nell'ultima major release di RSpec .

Come di RSpec 3.1, il modo corretto per verificare questo è:

expect("Some string".length).to be(11)

0

matchers Collection (tutti matchers che affermano contro stringhe, hash e array) sono stati astratti fuori in un gioiello individuale, rspec-collection_matchers .

Per poter utilizzare questi matchers aggiungere questo al vostro Gemfile:

gem 'rspec-collection_matchers' 

O il vostro .gemspec se stai lavorando su una gemma:

spec.add_development_dependency 'rspec-collection_matchers' 

Quindi, aggiungere questo al tuo spec_helper.rb:

require 'rspec/collection_matchers' 

E quindi sarete in grado di utilizzare matchers di raccolta nella vostra spec:

require spec_helper 
describe 'array' do 
    subject { [1,2,3] } 
    it { is_expected.to have(3).items } 
    it { is_expected.to_not have(2).items } 
    it { is_expected.to_not have(4).items } 

    it { is_expected.to have_exactly(3).items } 
    it { is_expected.to_not have_exactly(2).items } 
    it { is_expected.to_not have_exactly(4).items } 

    it { is_expected.to have_at_least(2).items } 
    it { is_expected.to have_at_most(4).items } 

    # deliberate failures 
    it { is_expected.to_not have(3).items } 
    it { is_expected.to have(2).items } 
    it { is_expected.to have(4).items } 

    it { is_expected.to_not have_exactly(3).items } 
    it { is_expected.to have_exactly(2).items } 
    it { is_expected.to have_exactly(4).items } 

    it { is_expected.to have_at_least(4).items } 
    it { is_expected.to have_at_most(2).items } 
end 

noti che è possibile utilizzare in modo intercambiabile items e characters, sono solo la sintassi di zucchero, e il have Matcher, e le sue varianti, può essere utilizzato su array, hash e la stringa .

Problemi correlati