Tiffani Ashley Bell
2008-Jul-07 21:27 UTC
[rspec-users] Not sure why this controller spec isn''t working
Hey folks,
I''ve been mocking and stubbing pretty nicely after the various bits of
advice I received earlier about doing so. I''ve come to bits of code
that
work in one place and not in another, though. I''m hoping it''s
not something
simple I''ve missed. The code below fails even though code that is
practically the same elsewhere (except different models in use) passes.
What gives?
I have:
in notes_controller_spec.rb:
before(:each) do
@mock_note = mock_model(Note, :body => "The hot dog shipment will be
in
later tonight.",
:organization => @mock_org)
@mock_org = mock_model(Organization, :name => "Slappy''s Hot
Dog Palace",
:notes => [@mock_note])
@notes = [@mock_note]
end
it "should render ''notes/new'' when the Note is setup with
invalid data, i.e.
without a body on POST create" do
Note.stub!(:new).and_return(@mock_note)
@notes.stub!(:<<).and_return(false)
post :create, :organization_id => @mock_org.id, :new_note => { :body
=> @mock_note.body }
response.should render_template("notes/new")
end
---
in notes_controller.rb:
def create
@new_note = Note.new(params[:new_note])
respond_to do |wants|
if @organization.notes << @new_note
wants.html { redirect_to organization_url(@organization) }
else
wants.html { render :action => "new" }
end
end
end
I figured that my stubbing the << method out in the test to return false
would do the job of triggering the proper branch of the if statement in the
controller action, but I''m not sure why it''s not doing so. I
get the error:
expected "notes/new", got nil. I have code in my Users controller
that does
the exact same thing with the << method and the tests pass
(they''re
virtually identical tests). Any thoughts? TIA.
--Tiffani AB
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20080707/cff347c9/attachment.html>
Britt Mileshosky
2008-Jul-07 21:55 UTC
[rspec-users] Not sure why this controller spec isn''t working
________________________________> Date: Mon, 7 Jul 2008 17:27:36 -0400 > From: tiffani2k3 at gmail.com > To: rspec-users at rubyforge.org > Subject: [rspec-users] Not sure why this controller spec isn''t working > > Hey folks, > > I''ve been mocking and stubbing pretty nicely after the various bits of advice I received earlier about doing so. I''ve come to bits of code that work in one place and not in another, though. I''m hoping it''s not something simple I''ve missed. The code below fails even though code that is practically the same elsewhere (except different models in use) passes. What gives? > > I have: > > in notes_controller_spec.rb: > > > before(:each) do > @mock_note = mock_model(Note, :body => "The hot dog shipment will be in later tonight.", > :organization => @mock_org) > @mock_org = mock_model(Organization, :name => "Slappy''s Hot Dog Palace", :notes => [@mock_note]) > > @notes = [@mock_note] > end > > > it "should render ''notes/new'' when the Note is setup with invalid data, i.e. without a body on POST create" do > Note.stub!(:new).and_return(@mock_note) > @notes.stub!(:< post :create, :organization_id => @mock_org.id, :new_note => { :body => @mock_note.body } > response.should render_template("notes/new") > end > > --- > > in notes_controller.rb: > > def create > @new_note = Note.new(params[:new_note]) > > respond_to do |wants| > if @organization.notes << @new_note > wants.html { redirect_to organization_url(@organization) } > else > wants.html { render :action => "new" } > end > end > end > > I figured that my stubbing the << method out in the test to return false would do the job of triggering the proper branch of the if statement in the controller action, but I''m not sure why it''s not doing so. I get the error: expected "notes/new", got nil. I have code in my Users controller that does the exact same thing with the << method and the tests pass (they''re virtually identical tests). Any thoughts? TIA. > > > --Tiffani ABWhere in your create action are you defining @organization? is this variable provided from a before filter? If not, that''s the first thing you should look at. Another thing I noticed is that your @mock_note defines its organization @mock_org before @mock_org is defined... thus probably setting @mock_note.organization to nil. _________________________________________________________________ The i?m Talkaton. Can 30-days of conversation change the world? http://www.imtalkathon.com/?source=EML_WLH_Talkathon_ChangeWorld
Zach Dennis
2008-Jul-07 22:06 UTC
[rspec-users] Not sure why this controller spec isn''t working
On Mon, Jul 7, 2008 at 5:55 PM, Britt Mileshosky <mileshosky at hotmail.com> wrote:> > > ________________________________ > > Date: Mon, 7 Jul 2008 17:27:36 -0400 > > From: tiffani2k3 at gmail.com > > To: rspec-users at rubyforge.org > > Subject: [rspec-users] Not sure why this controller spec isn''t working > > > > Hey folks, > > > > I''ve been mocking and stubbing pretty nicely after the various bits of > advice I received earlier about doing so. I''ve come to bits of code that > work in one place and not in another, though. I''m hoping it''s not something > simple I''ve missed. The code below fails even though code that is > practically the same elsewhere (except different models in use) passes. > What gives? > > > > I have: > > > > in notes_controller_spec.rb: > > > > > > before(:each) do > > @mock_note = mock_model(Note, :body => "The hot dog shipment will be > in later tonight.", > > :organization => @mock_org) > > @mock_org = mock_model(Organization, :name => "Slappy''s Hot Dog > Palace", :notes => [@mock_note]) > > > > @notes = [@mock_note] > > end > > > > > > it "should render ''notes/new'' when the Note is setup with invalid data, > i.e. without a body on POST create" do > > Note.stub!(:new).and_return(@mock_note) > > @notes.stub!(:< post :create, :organization_id => @ > mock_org.id, :new_note => { :body => @mock_note.body } > > response.should render_template("notes/new") > > end > > >To add to what Britt already said, look at your example and then your before block again very carefully. Notice that you setup @notes in your before, and you use it to stub :<< in your example, but you never return @notes from anything. Instead you setup @mock_org to return an array with @mock_note. Although the contents of this and @notes are the same, you have created two different arrays. You are probably wanting to be dealing with the same @notes object in each of these cases. Zach -- Zach Dennis http://www.continuousthinking.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080707/2c1945f5/attachment.html>
Tiffani Ashley Bell
2008-Jul-08 00:44 UTC
[rspec-users] Not sure why this controller spec isn''t working
Hmmm...I made the changes that both you and Britt suggested and still no dice. I broke it down and put print statements (yeesh) in the controller around the @organization.notes << @new_note statement. This confirms that the statement returns false just like the stub told it to do (so I took out the puts statement and ran the test again), but for some reason I still don''t get the rendered template as set up in the else clause. I''m not sure what''s going on, but I suppose I''ll just skip that test until later. Thanks for the help! Tiffani AB On Mon, Jul 7, 2008 at 6:06 PM, Zach Dennis <zach.dennis at gmail.com> wrote:> > On Mon, Jul 7, 2008 at 5:55 PM, Britt Mileshosky <mileshosky at hotmail.com> > wrote: > >> >> >> ________________________________ >> > Date: Mon, 7 Jul 2008 17:27:36 -0400 >> > From: tiffani2k3 at gmail.com >> > To: rspec-users at rubyforge.org >> > Subject: [rspec-users] Not sure why this controller spec isn''t working >> > >> > Hey folks, >> > >> > I''ve been mocking and stubbing pretty nicely after the various bits of >> advice I received earlier about doing so. I''ve come to bits of code that >> work in one place and not in another, though. I''m hoping it''s not something >> simple I''ve missed. The code below fails even though code that is >> practically the same elsewhere (except different models in use) passes. >> What gives? >> > >> > I have: >> > >> > in notes_controller_spec.rb: >> > >> > >> > before(:each) do >> > @mock_note = mock_model(Note, :body => "The hot dog shipment will be >> in later tonight.", >> > :organization => @mock_org) >> > @mock_org = mock_model(Organization, :name => "Slappy''s Hot Dog >> Palace", :notes => [@mock_note]) >> > >> > @notes = [@mock_note] >> > end >> > >> > >> > it "should render ''notes/new'' when the Note is setup with invalid data, >> i.e. without a body on POST create" do >> > Note.stub!(:new).and_return(@mock_note) >> > @notes.stub!(:< post :create, :organization_id => @ >> mock_org.id, :new_note => { :body => @mock_note.body } >> > response.should render_template("notes/new") >> > end >> > >> > > To add to what Britt already said, look at your example and then your > before block again very carefully. Notice that you setup @notes in your > before, and you use it to stub :<< in your example, but you never return > @notes from anything. Instead you setup @mock_org to return an array with > @mock_note. Although the contents of this and @notes are the same, you have > created two different arrays. You are probably wanting to be dealing with > the same @notes object in each of these cases. > > Zach > > > -- > Zach Dennis > http://www.continuousthinking.com > _______________________________________________ > 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/20080707/385b9b06/attachment-0001.html>
Craig Demyanovich
2008-Jul-08 02:31 UTC
[rspec-users] Not sure why this controller spec isn''t working
I''m assuming RSpec 1.1.3+, but here''s how I might write this
example:
describe NotesController do
it "renders ''notes/new'' when an empty note is
submitted" do
Note.stub!(:new).and_return(stub("note"))
organization = stub_model(Organization, :notes => stub("notes",
:<< => false))
assigns[:organization] = organization
post :create, "organization_id" => organization.to_param,
"new_note" => { "body" => "" }
response.should render_template("notes/new")
end
end
I''m favoring the use of stub_model and stubs instead of mock_model
(and mocks) because we''re not setting any expectations on the note or
the organization. Rather, we''re just setting them up to deliver values
that are either simply required (as in Note.new returning something
non-nil) or what we want (the addition of the note to the organization
to fail).
I''ve also eliminated the idea of having existing notes on the
organization. It''s not central to what''s being
spec''d, so it''s out.
Furthermore, I made the submitted note''s body empty because
that''s
what was in the original description of the it block. That those two
didn''t match bothered me.
Finally, I simplified the description of the it block to what I
thought the essence of your example was; I hope that I''m at least
close. :-)
Please let the list know if this helped or if you already revisited
the problem and solved it. I think we''re interested in what ends up
working for you.
Regards,
Craig
Tiffani Ashley Bell
2008-Jul-08 04:14 UTC
[rspec-users] Not sure why this controller spec isn''t working
Yeah, I was on 1.1.4 and this solution with a few tweaks definitely worked.
The original set up of my code called for having the Organization returned
by the notes controller in a before filter, so I stubbed out this stuff in
the before(:each) block. It ended up looking like this:
before(:each) do
@organization = stub_model(Organization, :name => "Slappy''s
Hot Dog
Palace")
Organization.stub!(:find).and_return(@organization)
end
Then, I followed Craig''s lead on what the example should do with a few
things taken out:
it "should render ''notes/new'' when an empty note is
submitted" do
Note.stub!(:new).and_return(stub("note"))
post :create, :appointment_id => @appointment.id, :organization_id => @
organization.id, :new_note => { :body => "" }
response.should render_template("notes/new")
end
I kept the controller method just like it was in the original post. And
whoooo! Everything passed. I''ll be making sure I really differentiate
the
necessity of mock_model vs. stub_model from now on, too as a take-away from
all this. Learning this stuff has really been interesting considering
months ago the only testing I was doing was making changes in Textmate and
then clicking "Refresh" in a browser. lol.
Thanks again!
Tiffani AB
On Mon, Jul 7, 2008 at 10:31 PM, Craig Demyanovich <cdemyanovich at
gmail.com>
wrote:
> I''m assuming RSpec 1.1.3+, but here''s how I might write
this example:
>
> describe NotesController do
> it "renders ''notes/new'' when an empty note is
submitted" do
> Note.stub!(:new).and_return(stub("note"))
> organization = stub_model(Organization, :notes =>
stub("notes",
> :<< => false))
> assigns[:organization] = organization
>
> post :create, "organization_id" => organization.to_param,
> "new_note" => { "body" => "" }
>
> response.should render_template("notes/new")
> end
> end
>
> I''m favoring the use of stub_model and stubs instead of mock_model
> (and mocks) because we''re not setting any expectations on the note
or
> the organization. Rather, we''re just setting them up to deliver
values
> that are either simply required (as in Note.new returning something
> non-nil) or what we want (the addition of the note to the organization
> to fail).
>
> I''ve also eliminated the idea of having existing notes on the
> organization. It''s not central to what''s being
spec''d, so it''s out.
>
> Furthermore, I made the submitted note''s body empty because
that''s
> what was in the original description of the it block. That those two
> didn''t match bothered me.
>
> Finally, I simplified the description of the it block to what I
> thought the essence of your example was; I hope that I''m at least
> close. :-)
>
> Please let the list know if this helped or if you already revisited
> the problem and solved it. I think we''re interested in what ends
up
> working for you.
>
> Regards,
> Craig
> _______________________________________________
> 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/20080708/d737da41/attachment-0001.html>
Tiffani Ashley Bell
2008-Jul-08 04:15 UTC
[rspec-users] Not sure why this controller spec isn''t working
Whoops..wrong post.
should be
post :create, :organization_id => @organization.id, :new_note => { :body
=>
"" }
Copying from the wrong window...
Tiffani AB
On Tue, Jul 8, 2008 at 12:14 AM, Tiffani Ashley Bell <tiffani2k3 at
gmail.com>
wrote:
> Yeah, I was on 1.1.4 and this solution with a few tweaks definitely
> worked. The original set up of my code called for having the Organization
> returned by the notes controller in a before filter, so I stubbed out this
> stuff in the before(:each) block. It ended up looking like this:
>
> before(:each) do
> @organization = stub_model(Organization, :name =>
"Slappy''s Hot Dog
> Palace")
> Organization.stub!(:find).and_return(@organization)
> end
>
> Then, I followed Craig''s lead on what the example should do with a
few
> things taken out:
>
> it "should render ''notes/new'' when an empty note is
submitted" do
> Note.stub!(:new).and_return(stub("note"))
>
> post :create, :appointment_id => @appointment.id, :organization_id
=> @
> organization.id, :new_note => { :body => "" }
> response.should render_template("notes/new")
> end
>
> I kept the controller method just like it was in the original post. And
> whoooo! Everything passed. I''ll be making sure I really
differentiate the
> necessity of mock_model vs. stub_model from now on, too as a take-away from
> all this. Learning this stuff has really been interesting considering
> months ago the only testing I was doing was making changes in Textmate and
> then clicking "Refresh" in a browser. lol.
>
> Thanks again!
> Tiffani AB
>
>
> On Mon, Jul 7, 2008 at 10:31 PM, Craig Demyanovich <cdemyanovich at
gmail.com>
> wrote:
>
>> I''m assuming RSpec 1.1.3+, but here''s how I might
write this example:
>>
>> describe NotesController do
>> it "renders ''notes/new'' when an empty note is
submitted" do
>> Note.stub!(:new).and_return(stub("note"))
>> organization = stub_model(Organization, :notes =>
stub("notes",
>> :<< => false))
>> assigns[:organization] = organization
>>
>> post :create, "organization_id" =>
organization.to_param,
>> "new_note" => { "body" => "" }
>>
>> response.should render_template("notes/new")
>> end
>> end
>>
>> I''m favoring the use of stub_model and stubs instead of
mock_model
>> (and mocks) because we''re not setting any expectations on the
note or
>> the organization. Rather, we''re just setting them up to
deliver values
>> that are either simply required (as in Note.new returning something
>> non-nil) or what we want (the addition of the note to the organization
>> to fail).
>>
>> I''ve also eliminated the idea of having existing notes on the
>> organization. It''s not central to what''s being
spec''d, so it''s out.
>>
>> Furthermore, I made the submitted note''s body empty because
that''s
>> what was in the original description of the it block. That those two
>> didn''t match bothered me.
>>
>> Finally, I simplified the description of the it block to what I
>> thought the essence of your example was; I hope that I''m at
least
>> close. :-)
>>
>> Please let the list know if this helped or if you already revisited
>> the problem and solved it. I think we''re interested in what
ends up
>> working for you.
>>
>> Regards,
>> Craig
>> _______________________________________________
>> 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/20080708/1990cc22/attachment.html>