require File.join(File.dirname(__FILE__), "validation_matcher_spec_support") # my desired shortcut syntax # # describe User, "validation to ensure that Oingo Boingo isn't used") do # before(:each) { @it = User.new } # validation_for(:firstname, :lastname) # reject_with "error message for rejections" # it_should accept("Nicholas", "Evans") # it_should accept( "David", "Chelimsky") # it_should reject( "Oingo", "Boingo") # end describe Person, "validations, using 'for' syntax" do include ValidationSpecHelper before(:each) { @it = Person.new } specify { @it.should reject(nil).for(:firstname) } specify { @it.should reject("").for(:firstname).with("can't be blank") } specify { @it.should accept("foo").for(:firstname) } specify { @it.should accept("bar").for(:firstname) } it "should reject a super long firstname" do @it.should reject("a" * 500).for(:firstname) end specify { @it.should reject("").for(:lastname) } specify { @it.should reject("aaa").for(:lastname).with("is too short (minimum is 4 characters)") } specify { @it.should reject("a" * 51).for(:lastname) } specify { @it.should accept("Nicholas", "Evans").for(:firstname, :lastname) } specify { @it.should accept("David", "Chelimsky").for(:firstname, :lastname) } specify { @it.should reject("Oingo", "Boingo").for(:firstname, :lastname) } specify { @it.should reject("Oingo", "Boingo").for(:firstname, :lastname). with("Sorry, that name is strictly forbidden!") } end describe Person, "validations, using 'of' syntax" do include ValidationSpecHelper before(:each) { @it = Person.new } specify { @it.should reject(:firstname).of(nil) } specify { @it.should reject(:firstname).of("").with("can't be blank") } specify { @it.should accept(:firstname).of("foo") } specify { @it.should accept(:firstname).of("bar") } specify { @it.should accept(:firstname, :lastname).of("Nicholas", "Evans") } specify { @it.should reject(:firstname, :lastname).of("Oingo", "Boingo"). with("Sorry, that name is strictly forbidden!") } end describe ValidationSpecHelper, "reverse validations (expecting the wrong things)" do include ValidationSpecHelper before(:each) { @it = Person.new } it "should fail when should accept is wrong, using 'of' syntax" do lambda {@it.should accept(:firstname).of("")}.should raise_error(Spec::Expectations::ExpectationNotMetError) end it "should fail when should accept is wrong" do lambda {@it.should accept("").for(:firstname)}.should raise_error(Spec::Expectations::ExpectationNotMetError) end it "should fail when should accept is wrong, with multiple fields" do lambda do @it.should accept("Oingo", "Boingo").for(:firstname, :lastname) end.should raise_error(Spec::Expectations::ExpectationNotMetError) end it "should fail when should reject is wrong" do lambda {@it.should reject("foo").for(:firstname)}.should raise_error(Spec::Expectations::ExpectationNotMetError) end it "should fail when should_not accept is wrong" do lambda {@it.should_not accept("foo").for(:firstname)}.should raise_error(Spec::Expectations::ExpectationNotMetError) end it "should fail when should_not reject is wrong" do lambda {@it.should_not reject("").for(:firstname)}.should raise_error(Spec::Expectations::ExpectationNotMetError) end it "should fail when the example is rejected but reason is not found in the list of errors" do lambda do @it.should reject("").for(:firstname).with("a non-existant reason") end.should raise_error(Spec::Expectations::ExpectationNotMetError) end end