我想測試我的控制器只是調用服務對象的方法。我認爲一個測試間諜會是完美的。我究竟做錯了什麼?謝謝你的幫助!如何在控制器測試中使用簡單的測試間諜?
控制器:
class PostsController < ApplicationController
def create
@post = Post.new(post_params)
if @post.save
ServiceObject.new(@post).call
redirect_to posts_path, success: "#{@post.title} was successfully created."
else
render action: :new
end
end
end
服務對象:
class ServiceObject
def initialize(post)
@post = post
end
def call
puts "Service object was called!"
end
end
控制器測試:
require 'spec_helper'
describe PostsController do
describe 'POST #create' do
it 'calls the service object' do
service_object = double(ServiceObject)
service_object.stub(:new).and_return(service_object)
service_object.stub(:call)
post :create, post: Fabricate.attributes_for(:post)
expect(service_object).to have_received(:call)
end
end
end
錯誤:
Failure/Error: expect(service_object).to have_received(:call)
(Double ServiceObject).call(any args)
expected: 1 time with any arguments
received: 0 times with any arguments
你檢查該職位被保存和'''如果@ post.save'''被返回true –