I’m new to rspec and looking for way to test a validation I added to a
model.
The test checks to see that if field1 has a value then field2 must be
nil
and vice versa.
When I did the rspec_scaffold it generated one test which worked
before :each do
@valid_attributes = {
:field1 = “value for field1”
:field2 = “value for field2”
}
MyTest.create!(@valid_attributes)
end
it “should create a new instance given valid attributes” do
MyTest.create!(@valid_attributes)
end
Before coding I modified the test file as follows
it “should create a new instance given valid attributes” do
@valid_attributes1 = {
:field1 = “value for field1”
:field2 = nil
}
MyTest.create!(@valid_attributes1)
@valid_attributes2 = {
:field1 = nil
:field2 = “value for field2”
}
MyTest.create!(@valid_attributes2)
end
it “should not create a new instance given incompatible attribute
values” do
@invalid_attributes1 = {
:field1 = “value for field1”
:field2 = “value for field2”
}
MyTest.create!(@invalid_attributes) # I don’t know how to test that
the
save failed!!
end
naturally the first two test failed and the last one as written didn’t.
after coding my validation the first two pass and naturally last one
didn’t.
How do I test that the create failed.
View this message in context:
http://www.nabble.com/rspec-model-testing---test-on-user-defined-validation--How-do-I-test-that-the-create-failed.-tp21465687p21465687.html
Sent from the rspec-users mailing list archive at Nabble.com.