Forum: RSpec rspec model testing - test on user defined validation- How do I test that the create failed.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
498f5bfbbab1d9a6ca9c220b3375a6e6?d=identicon&s=25 Ken Wegener (Guest)
on 2009-01-16 14:05
(Received via mailing list)
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-o...
Sent from the rspec-users mailing list archive at Nabble.com.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2009-01-16 17:19
(Received via mailing list)
On Fri, Jan 16, 2009 at 3:29 AM, Ken Wegener <kgwcoach@yahoo.com> wrote:
>  @valid_attributes = {
>
> MyTest.create!(@valid_attributes1)
>
>
> http://rubyforge.org/mailman/listinfo/rspec-users
>

Hi Ken,

I would write the specs like:

describe MyModel do
  it "should be valid when field1 is set and field2 is nil" do
    new_my_model(:field1 => "foo", :field2 => nil).should be_valid
  end

  it "should be valid when field2 is set and field1 is nil" do
    new_my_model(:field1 => nil, :field2 => "bar").should be_valid
  end

  it "should not be valid when field1 and field2 are set" do
    new_my_model(:field1 => "foo", :field2 => "bar").should_not be_valid
  end
end

new_my_model is coming from fixjour [1], but you could just use
MyModel.new if you want.  The key is that you get a new instance with
the attributes set as you want and then ask it if it's valid.

[1] http://github.com/nakajima/fixjour/tree/master
498f5bfbbab1d9a6ca9c220b3375a6e6?d=identicon&s=25 Wegener Ken (Guest)
on 2009-01-16 20:24
(Received via mailing list)
Solved my problem  all the examples I could find had the following
example
myTest.should_not_be_valid
but that method wasn't a valid method.

It appears the method has been refactored and the new form is
mytest.should_not(be_valid)  which does work

following test does what I want
it "should not create a new instance given incompatible attribute
values" do

@invalid_attributes1 = {
    :field1 = "value for field1"
    :field2 = "value for field2"
   }

mytest = MyTest.create(@invalid_attributes)
mytest.should_not(be_valid)
end

 Ken Wegener



----- Original Message ----
From: Ken Wegener <kgwcoach@yahoo.com>
To: rspec-users@rubyforge.org
Sent: Friday, January 16, 2009 6:29:19 AM
Subject: [rspec-users] rspec model testing - test on user defined
validation- How do I test that the create failed.


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-o...
Sent from the rspec-users mailing list archive at Nabble.com.

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2009-01-16 23:11
(Received via mailing list)
On Fri, Jan 16, 2009 at 8:51 AM, Wegener Ken <kgwcoach@yahoo.com> wrote:
> @invalid_attributes1 = {
>    :field1 = "value for field1"
>    :field2 = "value for field2"
>   }
>
> mytest = MyTest.create(@invalid_attributes)
> mytest.should_not(be_valid)
> end

Correct.  You can also write
mytest.should_not be_valid

which is a bit prettier :)

Pat
This topic is locked and can not be replied to.