I''m studying Rspec and doing some model''s tests. I have a table with a field which must have two characters, the field should not be empty and can not be repeated. My doubts is with #. describe State do context "validations" do it "The abbreviation should not be empty" do subject.abbreviation = nil subject.should_not be_valid end *# I can do like example below or I have to break in parts?* it "The abbreviation should have 2 characters" do subject.abbreviation = "BA" subject.should be_valid subject.abbreviation = "B" subject.should_not be_valid subject.abbreviation = "BAA" subject.should_not be_valid end it "The abbreviation can not be repeated" do * # I don''t know how to do!* end end end Thanks, Luciano -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120417/10b48e83/attachment.html>
What do you mean repeated? You mean you can''t have an abbreviation with two of the same letters? Julian On 18/04/2012, at 10:50 AM, Luciano Borges wrote:> I''m studying Rspec and doing some model''s tests. > > I have a table with a field which must have two characters, the field should not be empty and can not be repeated. > > My doubts is with #. > > describe State do > context "validations" do > > it "The abbreviation should not be empty" do > subject.abbreviation = nil > subject.should_not be_valid > end > > # I can do like example below or I have to break in parts? > > it "The abbreviation should have 2 characters" do > subject.abbreviation = "BA" > subject.should be_valid > > subject.abbreviation = "B" > subject.should_not be_valid > > subject.abbreviation = "BAA" > subject.should_not be_valid > end > > it "The abbreviation can not be repeated" do > > # I don''t know how to do! > > end > end > end > > Thanks, > Luciano > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120418/6bbd4f5f/attachment.html>
2012/4/17 Julian Leviston <julian at leviston.net>> What do you mean repeated? You mean you can''t have an abbreviation with > two of the same letters? > > Julian >Hi Julian, Yes. I cat''t have the same abbreviation on the table, I know that I have to put the validation in the model, but, I''d like to test first before change my model. Luciano -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120417/4ba10a28/attachment-0001.html>
oh you mean a check for uniqueness. Is this rails? If it is, pretty sure you can do this: item = Item.create(:abbreviation => "ab") duplicate_item = Item.create(:abbreviation => "ab") duplicate_item.new_record?.should_be true or item = Item.new(:abbreviation => "ab") item.should_be valid item.save duplicate_item = Item.new(:abbreviation => "ab") duplicate_item.should_not be_valid On 18/04/2012, at 11:11 AM, Luciano Borges wrote:> 2012/4/17 Julian Leviston <julian at leviston.net> > What do you mean repeated? You mean you can''t have an abbreviation with two of the same letters? > > Julian > > Hi Julian, > > Yes. I cat''t have the same abbreviation on the table, I know that I have to put the validation in the model, but, I''d like to test first before change my model. > > Luciano > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120418/e9aa899a/attachment.html>
On Apr 17, 2012, at 8:50 PM, Luciano Borges wrote:> I''m studying Rspec and doing some model''s tests. > > I have a table with a field which must have two characters, the field should not be empty and can not be repeated. > > My doubts is with #. > > describe State do > context "validations" do > > it "The abbreviation should not be empty" do > subject.abbreviation = nil > subject.should_not be_valid > end > > # I can do like example below or I have to break in parts? > > it "The abbreviation should have 2 characters" do > subject.abbreviation = "BA" > subject.should be_valid > > subject.abbreviation = "B" > subject.should_not be_valid > > subject.abbreviation = "BAA" > subject.should_not be_valid > end > > it "The abbreviation can not be repeated" do > > # I don''t know how to do! > > end > end > endWith things like the above, I usually want the rule, followed by some examples. I usually approach like below. Its nice to have it all right in the specdoc. describe ''validations'' do .... describe ''abbreviation'', ''must be two unique characters'' do specify ''AB is valid'' do subject.abbreviation = ''AB'' subject.should be_valid end specify ''A is not valid'' do subject.abbreviation = ''A'' subject.should_not be_valid end specify ''BAA is not valid'' do subject.abbreviation = ''BAA'' subject.should_not be_valid end I think it would be overkill for the above, but sometimes if I''m throwing lots of examples at something (e.g. email address validation), I''ll use a macro like below. describe ''abbreviation'', ''must be two unique characters'' do def self.it_is_valid(abbreviation) specify "#{abbreviation} is valid" do subject.abbreviation = abbreviation subject.should be_valid end end ..... it_is_valid(''AB'') it_is_valid(''BA'') it_is_not_valid(''AA'') it_is_not_valid(''BAA'') ..... -lenny> > Thanks, > Luciano > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120417/a7069bad/attachment.html>
Sorry! My posts are very delayed. On Apr 17, 2012, at 8:50 PM, Luciano Borges wrote:> I''m studying Rspec and doing some model''s tests. > > I have a table with a field which must have two characters, the field should not be empty and can not be repeated. > > My doubts is with #. > > describe State do > context "validations" do > > it "The abbreviation should not be empty" do > subject.abbreviation = nil > subject.should_not be_valid > end > > # I can do like example below or I have to break in parts?> > it "The abbreviation should have 2 characters" do > subject.abbreviation = "BA" > subject.should be_valid > > subject.abbreviation = "B" > subject.should_not be_valid > > subject.abbreviation = "BAA" > subject.should_not be_valid > end >Hadn''t seen your entity was State before. You meant abbreviation must be 2 characters and must be unique within the table. Anyway... my previous post still partially relevant for the example above.> it "The abbreviation can not be repeated" do > > # I don''t know how to do! > > endfor unique constraints, I use something like below describe ''code'' do .... it ''should be unique'' do attributes = @valid_attributes.merge(:code => ''foo'') AgeLevel.create!(attributes) AgeLevel.new(attributes).should_not be_valid end -lenny> end > end > > Thanks, > Luciano > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120417/b2aebc6d/attachment.html>