an_instance_of Mail ::消息未返回true

根据an_instance_of 匹配器上的这些文档 ,调用an_instance_of是另一种调用instance_of?(Class) 。 当我在控制台中测试时,这工作正常,但在我的规范中它失败了。

在控制台中:

 m = Mail.new => # m.instance_of?(Mail::Message) => true 

失败:

 1) IncomingEmailsController should deliver the email with lead info Failure/Error: post :create, to: "me@example.com", text: "Content", html: "HTML Content", from: "email@example.com", subject: "Jimmy Bean"  received :forward_email with unexpected arguments expected: (#) got: (#<Mail::Message:70334278571020, Multipart: true, Headers: , , , >, nil) 

incoming_emails_controller_spec.rb

 describe IncomingEmailsController do it "should deliver the email with lead info" do # expect UserMailer.should_receive(:forward_email).with(an_instance_of(Mail::Message)) # when post :create, to: "me@example.com", text: "Content", html: "HTML Content", from: "email@example.com", subject: "Jimmy Bean" end end 

incoming_emails_controller.rb

 def create # create a Mail object from the params sent by Sendgrid prepare_email(params) #returns @email (instance of Mail::Message) UserMailer.forward_email(@email).deliver end private def prepare_email(params) email = Mail.new email.to = params["to"] email.from = params["from"] email.subject = params["subject"] text_part = Mail::Part.new text_part.body = params["text"] html_part = Mail::Part.new html_part.content_type = 'text/html; charset=UTF-8' html_part.body = params["html"] email.text_part = text_part email.html_part = html_part @email = email end 

看起来这已经解决了: https : //github.com/rspec/rspec-mocks/issues/202 。