[rspec-users] fills_in NoMethodError

David Chelimsky dchelimsky at gmail.com
Thu Oct 9 06:34:03 EDT 2008

On Thu, Oct 9, 2008 at 1:57 AM, Matt Wynne <matt at mattwynne.net> wrote:
> 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.

Matt is correct, you should definitely consider moving to cucumber,
though I don't know that cucumber will necessary help you shed any
more light on this particular issue.

Mark, with the little bit of information you've provided, my best
guess would be that the :new action is providing some instance
variable that is not being provided by the :create action in the case
where the data from "When the user fills in the form" is incorrect or
insufficient to save the model.

If that's not correct, please post the code for the steps and the
relevant controller actions.


More information about the rspec-users mailing list