Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 检查行是否已使用Rspec执行_Ruby On Rails_Email_Testing_Rspec - Fatal编程技术网

Ruby on rails 检查行是否已使用Rspec执行

Ruby on rails 检查行是否已使用Rspec执行,ruby-on-rails,email,testing,rspec,Ruby On Rails,Email,Testing,Rspec,我有一个员工,当新的反馈突然出现时,他会向用户发送电子邮件。我想让用户不同意这一点(使用《狮子旗》)。问题是:如果执行了FedbackMailer.new\u feedback行,我如何测试(使用Rspec) account.users.each do |user| return if (user.no_notifications || user.just_summary) FeedbackMailer.new_feedback(account.id, feedback_i

我有一个员工,当新的反馈突然出现时,他会向用户发送电子邮件。我想让用户不同意这一点(使用《狮子旗》)。问题是:如果执行了
FedbackMailer.new\u feedback
行,我如何测试(使用Rspec)

  account.users.each do |user|
    return if (user.no_notifications || user.just_summary)

    FeedbackMailer.new_feedback(account.id, feedback_id, user.id).deliver_later
  end
你可以用

mailer=instance\u double
允许(反馈邮件程序)。接收(:新反馈)。使用(帐户id、反馈id、用户id)和返回(邮件程序)
允许(邮寄者)。接收(:稍后发送)
##做事##
期望(邮递员)。收到(以后发送)
如果您当时没有要传递的参数,也可以使用忽略


另一个解决方案是设置config
config.action\u mailer.delivery\u method=:test
并检查交付计数是否已更改


期待{
##发送电子邮件的代码
}.更改{ActionMailer::Base.deliveries.count}.by(1)

假设您的逻辑封装在类
MyClass

class MyClass
  def my_method
    account.users.each do |user|
      return if (user.no_notifications || user.just_summary)

      FeedbackMailer.new_feedback(account.id, feedback_id, user.id).deliver_later
    end
  end
end

RSpec.describe MyClass, type: :model do
  context "#my_method" do
    it "should send new feedback" do
      user_obj = create_user

      expect(user_obj.no_notifications).to be_falsey
      #OR
      #expect(user_obj.just_summary).to be_falsey

      account_obj = create_account
      account_obj.users << user_obj

      expect(account_obj.users).to include(user_obj)


      expect(FeedbackMailer).to receive(:new).with(account_obj.id, feedback_id, user_obj.id)
      # OR in case you don't have feedback_id then you can use
      # expect(FeedbackMailer).to receive(:new).with(account_obj.id, kind_of(Numeric), user_obj.id)

      # You should also setup expectation here that `FeedbackMailer` gets enqueued to ensure
      # that your method also gets invoked and the job also gets enqueued.

      subject.my_method
    end
  end
end
class-MyClass
def my_方法
account.users.each do | user|
返回if(user.no|u通知| user.just_摘要)
FeedbackMailer.new\u反馈(account.id,feedback\u id,user.id)。稍后交付
结束
结束
结束
RSpec.description MyClass,type::model do
上下文“#我的方法”执行
它“应该发送新的反馈”吗
用户\对象=创建\用户
预期(用户对象无通知)。为错误
#或
#期望(用户对象只是摘要)是错误的
账户\对象=创建\账户
用户帐户