an_instance_of Mail::Message 不返回 true [英] an_instance_of Mail::Message not returning true

查看:46
本文介绍了an_instance_of Mail::Message 不返回 true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to these docs on Rspec matchers, calling an_instance_of is another way of calling instance_of?(Class). When I test in the console, this works fine, but in my spec it fails.

在控制台中:

m = Mail.new
=> #<Mail::Message:70144097437100, Multipart: false, Headers: >
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"
       <UserMailer (class)> received :forward_email with unexpected arguments
         expected: (#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x007feff35fd148 @klass=Mail::Message>)
              got: (#<Mail::Message:70334278571020, Multipart: true, Headers: <From: email@example.com>, <To: me@example.com>, <Subject: Jimmy Bean>, <Content-Type: multipart/alternative; boundary=--==_mimepart_50c1ffa9e2ba2_2e1e3ff7f8c35ad49574>>, 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.

这篇关于an_instance_of Mail::Message 不返回 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆