【发布时间】:2017-12-22 19:26:43
【问题描述】:
我正在使用基于操作/服务的架构,其中服务类托管 HTTP 调用,操作类清理/打包数据并调用服务类以与外部 API 对话。设置工作正常,但我想知道如何为动作类设置模拟。这是一个示例操作/服务对:
# Service
class UserService
def update_user user_id, data
# make request
end
end
# Action
class UserUpdate
def initialize user_id, user_data
@id = user_id
@sanitized_data = user_data
end
def call
service = UserService.new
service.call(@id, @sanitized_data)
end
end
我可以毫无问题地模拟操作,但是当我尝试为服务使用类或实例替身时,我得到了错误。到目前为止,这是我的测试:
it "should create an instance of Agent Service" do
agent_action_mock = class_double("AgentUpdate")
agent_service_mock = class_double("AgentService")
allow(agent_update_mock).to receive_message_chain(:new, :call)
allow_any_instance_of(AgentService).to receive_message_chain(:initialize, :update_user)
agenta_action_mock.new(99, { status: "Verified" }).call
expect_any_instance_of(AgentService).to receive(:initialize)
end
这让我很开心Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure } Exactly one instance should have received the following message(s) but didn't: initialize
我想知道如何测试该操作是否正在创建服务类的实例并调用它的方法?
【问题讨论】:
-
但是你确定它真的被调用了吗?
标签: ruby-on-rails ruby unit-testing rspec