2

私のコントローラーは次のようになります。

def index
  params[:page]||1
  @stories = Story.all.page(params[:page]).per(5)
end

RSpec を使用してチェーン ラインのコントローラー テストを作成しようとすると、テストに合格できないようです。

私の controller_spec は次のようになります。

describe '#index' do
  let(:story) {double(Story)}
  before do
    allow(Story).to receive(:all).and_return(story)
    allow(story).to receive(:page).and_return(story)
    allow(story).to receive(:per).and_return(story)                                                                                                         
    get :index
  end
  context 'when user is not logged in' do
    it 'should get page 1' do                                                                                                
      expect(story).to receive(:page).with(1)                                                                                                               
    end
    it 'should get 5 stories' do
      expect(story).to receive(:per).with(5)                                                                                                                
    end
  end
end 

このようなコントローラー用に作成する良いサンプル テストは何でしょうか?

4

2 に答える 2

1

expect(story).to receive(:page).with(1)呼び出す前に設定する必要がありますget :index

get :indexブロックからbeforeブロックへ移動it:

it 'should get page 1' do                                                                                                
  expect(story).to receive(:page).with(1)
  get :index
end

=PS 、コントローラーのアクションを見逃したようです

params[:page] ||= 1
于 2014-06-20T08:56:37.430 に答える