モックとスタブでテストしていますが、コントローラーでcreate
アクションをテストするときに必要な出力が得られません。テスト時に正確な出力が得られないRSpecでアクションを作成する
失敗テスト:
describe 'authenticated user' do
let(:user) { instance_double(User) }
before do
allow(controller).to receive(:current_user) { user }
allow(controller).to receive(:authenticate_user!) { true }
end
describe "POST create" do
let(:achievement_params) { { title: "title" } }
let(:create_achievement) { instance_double(CreateAchievement) }
before do
allow(CreateAchievement).to receive(:new) { create_achievement }
end
it 'sends create message to CreateAchievement' do
expect(CreateAchievement).to receive(:new).with(achievement_params, user)
expect(create_achievement).to receive(:create)
post :create, achievement: achievement_params
end
end
end
コントローラでcreate
アクション
def create
service = CreateAchievement.new(params[:achievement], current_user)
service.create
render nothing: true
end
エラー:
1) AchievementsController authenticated user POST create sends create message to CreateAchivement
Failure/Error: expect(CreateAchievement).to receive(:new).with(achievement_params, user)
#<CreateAchievement (class)> received :new with unexpected arguments
expected: ({:title=>"title"}, #<InstanceDouble(User) (anonymous)>)
got: (<ActionController::Parameters {"title"=>"title"} permitted: false>, #<InstanceDouble(User) (anonymous)>)
Diff:
@@ -1,2 +1,3 @@
-[{:title=>"title"}, #<InstanceDouble(User) (anonymous)>]
+[<ActionController::Parameters {"title"=>"title"} permitted: false>,
+ #<InstanceDouble(User) (anonymous)>]