Richard G.
Richard G.

Reputation: 13

Receiving "unknown method" errors when running RSpec tests against an ActiveRecord model

I'm having a bit of trouble with some RSpec tests on some of my ActiveRecord validations. The test suite looks like this:

describe Event do
  context "An Event" do
    before do
       valid_event_hash = {
          :name => 'Blah Blah',
          :desc => 'Yadda Yadda Yadda',
          :category => 'some category',
          :has_partner => false,
          :event_abbr => 'BB'
       }
       @event = Event.new(valid_event_hash)
    end

    it "should have a name" do
       @event.name = ''
       @event.should_not be_valid
    end

    it "should have a description" do
       @event.desc = ''
       @event.should_not be_valid
    end

    it "should have an abbreviation no shorter than 2 letters and no longer than 3 letters" do
       @event.event_abbr = ''
       @event.should_not be_valid
       @event.event_abbr = 'BlaBla'
       @event.should_not be_valid
       @event.event_abbr = 'B'
       @event.should_not be_valid
    end

    after do
       @event.destroy
    end
 end

end

The model is set up so that it should pass all of these validations appropriately. The schema indicates that all of the fields I fill in are present and accounted for. Yet, when I run autotest, the tests fail with the following error:

Failure/Error: @event = Event.new(valid_event_hash)
unknown attribute: event_abbr

I can create the very same @event instance in the console with those values, and it works perfectly. My gut reaction is that, for some reason, the model that the test suite is using doesn't know about the :event_abbr field, but I can't think why that might be. I'm sure I'm missing something, but I'm not sure what it is. Any help would be greatly appreciated.

Upvotes: 1

Views: 559

Answers (1)

EnabrenTane
EnabrenTane

Reputation: 7466

Did you run your migrations on your test database? E.G.

RAILS_ENV=test rake db:migrate

else, try

rails console test

and try it there.

Upvotes: 2

Related Questions