我在编写一些规范时遇到了错误
QuestionsController GET #edit finds question to edit
Failure/Error: Question.should_receive(:find).with("#{question.id}").and_return(question)
(<Question(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime, user_id: integer, up_votes: integer, down_votes: integer) (class)>).find("9")
expected: 1 time
received: 2 times
class QuestionsController < ApplicationController
def edit
@question = Question.find(params[:id])
end
end
规范/控制器/questions_spec.rb
describe QuestionsController do
describe 'get edit' do
it 'finds question to edit' do
question = create(:question)
user = create(:user)
sign_in user
Question.should_receive(:find).and_return question
get :edit, :id => question.id
end
it 'renders edit template' do
question = create(:question)
user = create(:user)
sign_in user
Question.stub(:find).and_return question
get :edit, :id => question.id
expect(responce).to render_template 'edit'
end
end
end
我使用 Rspec、Factory Girl、database_cleaner、Postgres
spec_helper 中的 database_cleaner 配置
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do |group|
# The strategy needs to be set before we call DatabaseCleaner.start
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.use_transactional_fixtures = false
我正在测试编辑操作。我对问题设定了期望,以在第一个示例中接收 find 并在第二个示例中接收存根 find 方法调用。我在第一个示例中遇到了一个错误,我认为这两个示例并没有完全相互隔离。