2017-05-11 4 views
1

では使用できません。 RSpec(3.5.0)、FactoryGirl(4.8.0)、DatabaseCleaner(1.5.3)、Mongoid(6.0.3)を使用しています。レール - FactoryGirlによって持続オブジェクトは、私は管理者の名前空間での私のコントローラのテストを書いていコントローラ

問題は、これらのテスト行為妙ということです。 GET indexリクエストをテストすると、FactoryGirlによって生成されたオブジェクトが正常に作成され、永続化されます。しかし、コントローラはそれらを見つけるように見えません。

私は、3つの異なるコントローラを持っています。 3人中2人がこの問題を抱え、3人目が魅力のように働きます。コードは同じです(命名を除いて)唯一の違いは、現用コントローラーのリソースがネストされていることです。

アクセサリーのための1つの作品:

describe "GET #index", get: true do 

    let (:accessory) { FactoryGirl.create(:accessory) } 

    before do 
     get :index, params: { category_id: accessory.category_id.to_s }, session: valid_session, format: :json 
    end 

    it "responses with OK status" do 
     expect(response).to have_http_status(:success) 
    end 

    it "responses with a non-empty Array" do 
     expect(json_body).to be_kind_of(Array) 
     expect(json_body.length).to eq(1) 
    end 

    it "responses with JSON containing accessory" do 
     expect(response.body).to be_json 
     expect(json_body.first.with_indifferent_access).to match({ 
      id: accessory.to_param, 
      name: 'Test accessory', 
      description: 'This is an accessory', 
      car_model: 'xv', 
      model_year: '2013', 
      images: be_kind_of(Array), 
      category_id: accessory.category.to_param, 
      dealer_id: accessory.dealer.to_param, 
      url: be_kind_of(String) 
     }) 
    end 
    end 

とカテゴリに1つにはない:

describe "GET #index", get: true do 

    let (:category) { FactoryGirl.create(:category) } 

    before do 
     get :index, params: {}, session: valid_session, format: :json 
    end 

    it "responses with OK status" do 
     expect(response).to have_http_status(:success) 
    end 

    it "responses with a non-empty Array" do 
     expect(json_body).to be_kind_of(Array) 
     expect(json_body.length).to eq(1) 
    end 

    it "responses with JSON containing category" do 
     expect(response.body).to be_json 
     expect(json_body.first.with_indifferent_access).to match({ 
      id: category.to_param, 
      name: 'Test category', 
      image: be_kind_of(String), 
      url: be_kind_of(String) 
     }) 
    end 
    end 

あなたがロジックを見ることができるように同じである:beforeフックに要求を発行し、 letを使用してオブジェクトを設定します。

別の奇妙なことは、同じロジックを持つカテゴリのGET showテストは完璧に動作していることです。これらの質問に

12)彼らはそれがDatabaseCleaner戦略によるものかもしれないし、1の代わりにtransaction戦略のtruncationを使用する必要がありますと言います。モンゴイエは、truncationしか許していないので、私はそれをします。そして、私はまた、FactoryGirlとDatabaseCleanerためuse_transactional_fixtures = false

RSpecのコンフィグにJavaScript対応のテスト、具体的に語ったRSpecのを使用していない:

RSpec.configure do |config| 
    config.include FactoryGirl::Syntax::Methods 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :truncation 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each, :js => true) do 
    DatabaseCleaner.strategy = :truncation 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 

私はこれらのテストは、要求を発行して、オブジェクトを作成することによって、合格作ることができていますそれぞれの例ではbeforeletの代わりに使用します。しかし、私はそれが彼らと一緒に働くべきだと思います。

コントローラのインデックスメソッドはデフォルトです:

def index 
    @thing = Thing.all 
end 

あなたはこの奇妙な動作上の任意の考えを持っていますか?

答えて

3

let!代わりのletてみてください。

letは遅延評価されています。カテゴリデータは、category.to_paramに電話すると生成されます。ブロックbeforeには存在しません。

も参照してください。https://relishapp.com/rspec/rspec-core/v/3-5/docs/helper-methods/let-and-let

+0

右です。実践例で実際にオブジェクトを呼んでいることに気づいていないとは信じられません。ありがとう、@ザル! – mityakoval

関連する問題