简体   繁体   English

RSpec对ActiveMailer模型的测试调用

[英]RSpec Test Call to ActiveMailer model

Hey everyone I am trying to get Rspec to test if an action calls a specific method in a model I have which inherits from ActiveMailer but have been having no luck. 大家好,我正在尝试让Rspec测试操作是否在我拥有的模型中调用了特定方法,该模型继承自ActiveMailer但没有运气。 So as a quick mockup I have the following scenario. 因此,作为一个快速的模型,我有以下情形。 Model UserNotifier: 型号UserNotifier:

class UserNotifier < ActionMailer::Base  
  def foobaz
  end
end

Controller Password Controller: 控制器密码控制器:

class PasswordsController < ApplicationController

def foobar
  UserNotifier.foobaz
end

And the spec: 和规格:

 describe "GET 'foobar'" do

   it "should call the UserNotifier foobaz method" do
     UserNotifier.should_receive(:foobaz)
     get :foobar
   end
 end

but I always end up with this failure: 但我总是以失败告终:

1) PasswordsController GET 'foobar' should call the UserNotifier foobaz method
 Failure/Error: UserNotifier.should_receive(:foobaz)
   (<UserNotifier (class)>).foobaz(any args)
       expected: 1 time
       received: 0 times

Can anyone enlighten me as to why RSpec does not register that the UserNotifier.foobaz method is being called? 谁能启发我为什么RSpec没有注册UserNotifier.foobaz方法正在被调用?

Answered in the comments by Ryan Bigg. 在Ryan Bigg的评论中回答。 Ended up being a before_filter which was causing the method to never be run 最终是一个before_filter,导致该方法永远无法运行

I've got exactly the same problem, but it's not due to any filters (not using any). 我遇到了完全相同的问题,但这不是由于任何过滤器(不使用任何过滤器)引起的。 One difference is that I'm calling an action on ApplicationController, though I don't see why that would matter. 一个区别是我在ApplicationController上调用了一个动作,尽管我不明白为什么这很重要。 The foobaz method definitely gets called, given that the rspec run includes output I insert there, yet Rspec doesn't see the call. 鉴于rspec运行包含我在其中插入的输出,因此foobaz方法肯定会被调用,但是Rspec看不到该调用。 Any other ideas for what could be wrong? 还有什么其他想法可能会出错?

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

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