5

I have a controller spec something like this

describe :bizzaro_controller do

  let(:credit_card_account) { FactoryGirl.build :credit_card_account }

  it "doesn't blow up with just the stub" do
    CreditCardAccount.stub(:new).and_return(credit_card_account)
  end

  it "doesn't blow up" do
    credit_card_account
    CreditCardAccount.stub(:new).and_return(credit_card_account)
  end

end

Which results in this:

bizzaro_controller
  doesn't blow up with just the stub (FAILED - 1)
  doesn't blow up

Failures:

  1) bizzaro_controller doesn't blow up
     Failure/Error: let(:credit_card_account) { FactoryGirl.build :credit_card_account }
     NoMethodError:
       undefined method `exp_month=' for nil:NilClass
     # ./spec/controllers/user/bizzareo_controller_spec.rb:5:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/user/bizzareo_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

Finished in 0.23631 seconds
2 examples, 1 failure

My credit card factory looks like this:

FactoryGirl.define do
  factory :credit_card_account do
    exp_month 10
    exp_year 2075
    number '3'
  end
end

My CreditCardAccount is an empty ActiveRecord::Base model

=> CreditCardAccount(id: integer, exp_month: integer, exp_year: integer, number: string)

Versions

0 HAL:0 work/complex_finance % bundle show rails rspec-rails factory_girl
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/rails-4.0.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/rspec-rails-2.14.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/factory_girl-4.2.0
4
  • all looks legit to me. Have you prepared your test database properly, ie. rake db:test:prepare? Oct 31, 2013 at 3:05
  • Yeah. Did a RAILS_ENV=test rake db:wipe db:migrate db:seed before. (db:wipe is a custom task that just drops all the tables) Oct 31, 2013 at 3:25
  • 1
    can you try actually running a rake db:test:prepare? Oct 31, 2013 at 4:48
  • @sevenseacat Done. No change. Nov 2, 2013 at 3:19

3 Answers 3

0

This should be working. all points that your test database is not correct.

RAILS_ENV=test rake db:drop db:create will drop and recreate your test database. Then try to run your rspec using the rake command, in order to migrate the database: rake rspec

3
  • Thanks, but that wasn't it. Oct 31, 2013 at 2:49
  • @DeanBrundage sorry. Will let the answer there to possible help someone that google to here. Can you see this column in your test database? is your db/schema.rb correct?
    – fotanus
    Oct 31, 2013 at 2:49
  • See my reply to your answer. Oct 31, 2013 at 3:41
0

I was having the same problem, but I think the cause of my problem was different. My solution, however, may perhaps be useful: I used the Fabrication gem (http://www.fabricationgem.org/) instead of FG.

The reason why I was having this problem was because I was trying to have FG create/build an object that was not ActiveRecord, it was only an ActiveModel, and it had to be initialized with arguments.

I didn't see in the Fabricator documentation an example totally like what I needed, but I got it with this syntax:

Fabricator(:my_class) do on_init do init_with("Company Name", "Fake second arg") end end

0

My problem was that in model I made a private method called :send (forgot that it is already used in Ruby).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.