Fills_in NoMethodError

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.

Does this ring any bells with anyone?

Mark.

On 9 Oct 2008, at 02:55, Mark T. wrote:

Then the updated form should be displayed
NoMethodError: You have a nil object when you didn’t expect it! You
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.

On Thu, Oct 9, 2008 at 1:57 AM, Matt W. [email protected] wrote:

When the user fills in the form

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.

Cheers,
David

Thanks guys. I took your advice and moved to cucumber. It did turn out
to be useful in that the more verbose error reporting led me to dive
more deeply into the webrat source, eventually leading me to my problem

  • which actually was essentially the same noob error I made a few days
    when I failed to realize my development code had a data migration that
    hadn’t populated the test db.

In this case the updated form mentioned in my earlier post had selection
inputs whose options are meant to be stored in the db. Since they
weren’t actually there, there were no select options in the form.

What was puzzling initially was that an error related to selection
inputs was showing up in the fills_in call. But it turns out that webrat
looks for the named text field by searching over all the input tags in
the form. In the process of parsing the selection inputs it was throwing
up when it didn’t find the option tags it was expecting.

Mark.

Do we need to make it a bit clearer on the rspec.info site that
cucumber is the way forward? The trail of blog posts etc with the up-
to-date info is probably fairly bewildering to a newbie.

Yeah, I think that would be a very good idea. Some info on how Webrat
fits in with RSpec would be helpful also.

FWIW, from the perspective of someone coming to this with absolutely no
past Rails testing experience and only a few months of RoR experience,
here’s what I saw when I started looking into stories for the first time
at the beginning of the week…

  1. the rspec.info home page begins with a description of how stories
    work in the old framework.

  2. the most detailed information available on how to actually implement
    story-based testing applies to the old (or older) framework. In
    particular, the following were critical for me to get going (not all
    referenced on rspec.info) -
    http://blog.davidchelimsky.net/articles/2007/10/21/story-runner-in-plain-english
    http://blog.emson.co.uk/2008/06/understanding-rspec-stories-a-tutorial/
    http://www.tomtenthij.co.uk/2008/1/25/rspec-plain-text-story-runner-on-a-fresh-rails-app
    http://www.vaporbase.com/postings/Beginners_Guide_to_Rspec_on_Story_Runner
    eli.st

  3. David’s 9/22 blog post (http://blog.davidchelimsky.net/) seemed to
    indicate that cucumber was for the future and suggested (to me) that it
    might be premature for mainstream use before Rails 2.2.

  4. Aslak’s story migration posting seemed to involve a whole bunch of
    new things I was going to have to learn (of course it doesn’t really)

So for me, just struggling to get my arms around how this thing works,
Cucumber just looked like an unnecessary step too far. Of course I see
now that it’s not such a big deal, but that sure wasn’t obvious at the
start.

Incidentally, I would not have come to RSpec at all had it not been for
Obie F.’ book.

Mark.

On Fri, Oct 10, 2008 at 3:05 AM, Matt W. [email protected] wrote:

Do we need to make it a bit clearer on the rspec.info site that cucumber is
the way forward? The trail of blog posts etc with the up-to-date info is
probably fairly bewildering to a newbie.

I updated the website (just a quick few tweaks).

FYI - the website is in the rspec-dev project on github:

http://github.com/dchelimsky/rspec-dev

Please feel absolutely free at any time to contribute patches to this
if you learn info that you think should be up there.

Thanks,
David