2013-10-31 16 views
5

私は、コントローラの仕様にこの、その結果、このnilのためのファクトリー・ガール未定義のメソッドは:NilClass

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 

のようなものを持っている:

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 

私のクレジットカードの工場は次のようになります。

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

私のCreditCardAccountは空のActiveRecord :: Baseモデル

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

バージョン

0 HAL:0 work/complex_finance % bundle show rails rspec-rails factory_girl 
/home/brundage/.rvm/gems/[email protected]_finance/gems/rails-4.0.0 
/home/brundage/.rvm/gems/[email protected]_finance/gems/rspec-rails-2.14.0 
/home/brundage/.rvm/gems/[email protected]_finance/gems/factory_girl-4.2.0 
+0

すべてが正式に見えます。テストデータベースを適切に準備しましたか? 'rake db:test:prepare'? – sevenseacat

+0

うん。前に 'RAILS_ENV = rake db:wipe db:migrate db:seed'を実行しました。 (db:wipeはすべてのテーブルを削除するカスタムタスクです) –

+1

実際に 'rake db:test:prepare'を実行できますか? – sevenseacat

答えて

0

は、これは動作するはずです。あなたのテストデータベースが正しくないすべての点。

RAILS_ENV=test rake db:drop db:createテストデータベースを削除して再作成します。次に、データベースを移行するためにrakeコマンドを使用してrspecを実行してみてください。rake rspec

+0

ありがとう、それはそうではありませんでした。 –

+0

@DeanBrundage申し訳ありません。そこにGoogleに誰かを助ける可能性のある答えを出すだろう。あなたのテストデータベースでこの列を見ることができますか?あなたの 'db/schema.rb'は正しいですか? – fotanus

+0

両方ともYesです。 。 。 –

0

私は同じ問題を抱えていましたが、私の問題の原因は異なっていました。しかし、私の解決策はおそらく役に立つかもしれません:FGの代わりにFabrication gem(http://www.fabricationgem.org/)を使用しました。

私がこの問題を抱えていた理由は、ActiveRecordではないオブジェクトをFGで作成/構築しようとしていたため、ActiveModelだけであり、引数で初期化しなければならなかったからです。私は完全に私が必要なもののような加工業者のマニュアルに例を見ませんでしたが、私はこの構文でそれを得た

加工業者(:my_class) は init_with(「会社名」を行うon_initんが、 「フェイク第二引数」) 終了 エンド

0

私の問題は、モデルに、私は(それが既にRubyで使用されていることを忘れてしまった):sendと呼ばれるプライベートメソッドを作ったということでした。

関連する問題