简体   繁体   English

rspec测试调用方法发送重新确认指令

[英]Rspec test call method send reconfirmation instruction

I'm using Rspec to test the case when user change password, mail will be sent.我正在使用 Rspec 来测试用户更改密码时的情况,将发送邮件。 And I want to check that only 1 mail is sent.我想检查是否只发送了 1 封邮件。 I don't want use Action::Mailer.deliveries to check, instead I want to check that whether method is called and how much.我不想使用Action::Mailer.deliveries来检查,而是想检查是否调用了方法以及调用了多少。

On searching I found Test Spy from rspec mock: https://github.com/rspec/rspec-mocks#test-spies在搜索时,我从 rspec mock 中找到了Test Spyhttps : //github.com/rspec/rspec-mocks#test-spies

describe 'PUT /email' do
  include_context 'a user has signed in', { email: 'old-email@example.com', password: 'correct_password' }

  context  'correct new email, correct password' do
    before do
      allow(user).to receive(:send_reconfirmation_instructions)
    end

    it do
      should == 302
      expect(user.reload.unconfirmed_email).to eq 'new-email@example.com'
      expect(user).to receive(:send_reconfirmation_instructions).once
    end
  end
end

But I got error:但我得到了错误:

  Failure/Error: expect(user).to receive(:send_reconfirmation_instructions)

   (#<User id: 1269, email: “old-email@example.com”, created_at: “2019-08-27 03:54:33", updated_at: “2019-08-27 03:54:33”...“>).send_reconfirmation_instructions(*(any args))
       expected: 1 time with any arguments
       received: 0 times with any arguments

send_reconfirmation_instructions this function is from devise: https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb#L124-L130 send_reconfirmation_instructions这个函数来自send_reconfirmation_instructionshttps : //github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb#L124-L130

I did binding.pry and I'm sure that the test jump inside this function but rspec still failed.我做了binding.pry并且我确定这个函数内部的测试跳转但是 rspec 仍然失败。

Edit: I could make it kind of work by writing like this:编辑:我可以这样写:

describe 'PUT /email' do
  include_context 'a user has signed in', { email: 'old-email@example.com', password: 'correct_password' }

  context  'correct new email, correct password' do
    before do
      expect_any_instance_of(User).to receive(:send_reconfirmation_instructions).once
    end

    it do
      should == 302
      expect(user.reload.unconfirmed_email).to eq 'new-email@example.com'
      # expect(user).to receive(:send_reconfirmation_instructions).once
    end
  end
end

However I got another error:但是我遇到了另一个错误:

Failure/Error: 
(#<User user_id: 1418, email: “old-email@example.com”...“>).send_reconfirmation_instructions(#<User user_id: 1418, email: “old-email@example.com” ...“>)
                expected: 1 time with any arguments
                received: 2 times with arguments: (#<User user_id: 1418, email: “old-email@example.com”, id: 1323...“>)

The line where you're checking that the user has received the message should read:您检查用户是否收到消息的行应为:

expect(user).to have_received(:send_reconfirmation_instructions).once

instead of:代替:

expect(user).to receive(:send_reconfirmation_instructions).once

The former where you say expect(obj).to have_received(:msg) requires you to assert that the message was called, as you intended to do.前者你说expect(obj).to have_received(:msg)要求你断言消息被调用,正如你打算做的那样。

The latter, on the other hand, where you say expect(obj).to receive(:msg) is a way to set up the expectation before the action, ie in lieu of the allow(obj).to receive(:msg) without requiring to assert that it was called after the action.另一方面,后者,你说expect(obj).to receive(:msg)是一种在动作之前设置期望的方法,即代替allow(obj).to receive(:msg)无需断言它是在操作之后调用的。 After the spec ran, it will automatically assert whether it was called.规范运行后,它会自动断言它是否被调用。

This explains the error you're getting when specifying这解释了您在指定时遇到的错误

expect(user).to receive(:send_reconfirmation_instructions).once

as no code after that line sends that message to user , which gets verified after the spec.因为在该行之后没有代码将该消息发送给user ,该消息在规范之后得到验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM