【问题标题】:Rspec how to checks the value present in the an another method which that method doesn't return anything?Rspec如何检查该方法不返回任何内容的另一个方法中存在的值?
【发布时间】:2022-01-02 15:49:56
【问题描述】:

您好,我已经编写了 Rspec 来验证消息体\n

 describe "validate_msm" do

  let!(:user) { FactoryGirl.create(:user) }
  it "should contains welcome user" do 
    body = user.send_sms("909090990")
    expect(body).to include("welcome")
  end
end

因为 send_sms 会调用我在 let 中提到的 send 方法!

def send_sms

...

..

  body="welcome user"

  Message.send(contact, body)

end

所以如何检查正文内容等于欢迎用户, 由于 send_sms 不返回任何内容,因此如何检查 rspec 中 body 变量中存在的值

【问题讨论】:

    标签: ruby-on-rails ruby rubygems


    【解决方案1】:

    你没有。或者说你没有那么容易。大多数像这样的库应该与测试适配器和帮助程序一起使用,以使这样的测试成为可能。如果这个没有,您只能测试消息是否已使用正确的参数发送:

    it "should contains welcome user" do
      allow(Message).to receive(:send)
      
      user.send_sms("909090990")
      
      expect(Message).to have_received(:send) do |_contact, body|
        expect(body).to include "welcome"
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多