我正在使用模拟和存根进行测试,但create
在控制器中测试操作时无法获得所需的输出。
失败的测试:
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)>]