我正在尝试测试Facebook僵尸程序,但无法弄清楚如何检查方法是否被调用。语法似乎是正确的,当我在pry会话中调用该方法时,它按预期工作。
测试:
RSpec.describe Postback do
describe "initialize" do
before do
@fb_postback = get_postback
@property = get_property
@tenancy = get_tenancy(@property.id)
@tenant = get_tenant
@postback = Postback.new(@fb_postback.payload, @tenant, @tenancy.id, @tenancy.pin)
end
describe "process" do
context "when it's a new thread and the user is not authenticated" do
before do
@postback.process
end
it "should call the tenant_returning method" do
expect(@postback.introduction).to receive(:send_onboard_and_get_tenant_type).with(no_args)
end
end
end
end
end
前一块中的方法是工厂方法
班级:
class Postback
attr_reader :payload, :tenant, :general, :authentication, :introduction
def initialize(payload, tenant, ref_tenancy_id, ref_pin)
@payload = payload
@tenant = tenant
@tenancy = Tenancy.find_by(id: ref_tenancy_id)
@general = General.new(@tenant, @tenancy)
@authentication = Authentication.new(@tenant, @tenancy, ref_pin)
@introduction = Introduction.new(@tenant, @tenancy)
end
def process
case payload
when "new_thread"
if authentication.state && authentication.state.current_state == "authenticated"
general.tenant_returning
else
if authentication.passed?
introduction.send_onboard_and_get_tenant_type
else
authentication.failed
end
end
when /tenant_type=(\w+)/
introduction.respond_to_tenant_type($1)
end
end
end
错误:
Failures:
1) Postback initialize process when it's a new thread and the user is not authenticated should call the tenant_returning method
Failure/Error: expect(@postback.introduction).to receive(:send_onboard_and_get_tenant_type).with(no_args)
(#<Introduction:0x007fec3f2b4930 @tenant=#<Tenant id: 51, email: nil, created_at: "2017-03-17 11:42:46", updated_at: "2017-03-17 11:42:46", fb_id: nil, first_name: nil, last_name: nil, gender: nil, locale: nil, timezone: nil, tenant_type: nil, authenticated?: nil>, @tenancy=#<Tenancy id: 61, property_id: 58, created_at: "2017-03-17 11:42:46", updated_at: "2017-03-17 11:42:46", start_date: nil, pin: "d68d054c1e8ecb69cc22">, @flow=#<Flow id: 120, name: "Introduction", created_at: "2017-03-17 11:42:46", updated_at: "2017-03-17 11:42:46">, @tenancy_tenant=nil>).send_onboard_and_get_tenant_type(no args)
expected: 1 time with no arguments
received: 0 times with no arguments
# ./spec/bot/postback_spec.rb:34:in `block (5 levels) in <top (required)>'
由于
答案 0 :(得分:0)
解决方案 - 方法调用的期望必须在之前发生,所以我只是在expect子句之后移动了@postback.process
。
感谢Jan Klimo!