2

1 つの機能一覧ページの統合テスト ケースを作成する必要があり、その機能インデックス メソッドには以下のようなコードがあります。

def index
  @food_categories = current_user.food_categories
end

これのテストケースを書き込もうとすると、エラーがスローされます

'undefined method features for nil class' because it can not get the current user

今私がしていることは以下のとおりです。各ステートメントの前にログインプロセスを記述し、機能リストページのテストケースを記述しました

を入手する方法を教えてくださいcurrent_user

参考までに、私はdevise gemを使用し、Rspecとの統合テストケースに取り組んでいます

ここに私のspecファイルがあります そしてここに私のfood_categories_spec.rbがあります

4

1 に答える 1

3

更新:機能テストと統合テストを混同しています。getテストするコントローラ アクションがないため、統合テストでは を使用しません。代わりにvisit(some url) を使用する必要があります。次に、応答コードではなく、ページのコンテンツを調べる必要があります (後者は機能テスト用です)。次のようになります。

visit '/food_categories'
page.should have_content 'Eggs'
page.should have_content 'Fats and oils'

機能テストが必要な場合の例を次に示します。

# spec/controllers/your_controller_spec.rb
describe YourController do

  before do
    @user = FactoryGirl.create(:user)
    sign_in @user
  end

  describe "GET index" do

    before do
      get :index
    end

    it "is successful" do
      response.should be_success
    end

    it "assings user features" do
      assigns(:features).should == @user.features
    end
  end
end

# spec/spec_helper.rb
RSpec.configure do |config|
  #...
  config.include Devise::TestHelpers, :type => :controller
end
于 2012-04-06T06:34:16.743 に答える