7

私はコントローラーの仕様を書いています:

it 'should call the method that performs the movies search' do
  movie = Movie.new
  movie.should_receive(:search_similar)
  get :find_similar, {:id => '1'}
end

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

def find_similar
 @movies = Movie.find(params[:id]).search_similar
end

rspecを実行した後、次のようになります。

Failures:
1) MoviesController searching by director name should call the method that performs the movies search
 Failure/Error: movie.should_receive(:search_similar)
   (#<Movie:0xaa2a454>).search_similar(any args)
       expected: 1 time
       received: 0 times
 # ./spec/controllers/movies_controller_spec.rb:33:in `block (3 levels) in <top (required)>'

これは理解して受け入れているようです。コントローラーコードでClass(Movie)メソッドを呼び出しても、仕様で作成されたオブジェクトに「find_similar」を接続する方法が見当たらないためです。

だから問題は->メソッドが仕様で作成されたオブジェクトで呼び出されているかどうかを確認する方法は何ですか?

4

2 に答える 2

7
it 'should call the method that performs the movies search' do
  movie = Movie.new
  movie.should_receive(:search_similar)
  Movie.should_receive(:find).and_return(movie)
  get :find_similar, {:id => '1'}
end

価値のあることとして、私はこれらのスタブオールシングステストに完全に反対しています。これらはコードの変更を難しくし、実際にはコード構造のみをテストしています。

于 2012-04-07T17:00:12.527 に答える
0

最初は、ムービーはまだ永続化されていません。

第二に、事実ではなく、それはIDを持ちます1

だからこれを試してみてください

it 'should call the method that performs the movies search' do
  movie = Movie.create
  movie.should_receive(:search_similar)
  get :find_similar, {:id => movie.id}
end
于 2012-04-07T16:20:48.100 に答える