[rspec-users] fills_in NoMethodError

Matt Wynne
Thu, 09 Oct 2008 06:57:37 -0000

On 9 Oct 2008, at 02:55, Mark Thomson wrote:

> I have an RSpec story with an overall structure that looks something
> like this -
>
> ----
> Given the user is on the start page
> When the user clicks the ''new'' button
> Then the ''new record'' page should be displayed
>
> When the user fills in the form
> And the user submits the form
> Then the updated form should be displayed
>
> When the user fills in the updated form
> And the user submits the form
> Then a conformation message should be displayed
> ----
>
> In the last When step, I have a fills_in call that is failing with
> the following:
>
> ----
> NoMethodError: You have a nil object when you didn''t expect it!
You
> might have expected an instance of ActiveRecord::Base. The error
> occurred while evaluating nil.attributes.
> ----
>
> I think the ActiveRecord thing is a red herring. Poking through the
> Webrat source code, it seems like the problem is more likely a
> failure to find any input fields. But I''m not sure why. Is there
> anything inappropriate with the way I''ve organized the story,
> stringing together multiple When-Then segments? One thing that seems
> curious is that I have a completely identical fills_in call in the
> When step that precedes the one that is failing and yet that one
> runs without any problem.

I think this sort of error tends to happen while a view is being
rendered, so my guess is it''s something to do with your data setup.

Sorry to bang on about this, but cucumber has much better support for
error messages - you might want to consider making the switch before
you waste too much time on this issue.