简体   繁体   English

Rails如何测试方法使用Rspec接收块参数

[英]Rails How to test a method receives block argument with Rspec

Lets say I have this in application controller: 可以说我在应用程序控制器中有这个:

class ApplicationController < ActionController::Base

    def example(&block)
        ...code here
    end
end

and this in another controller: 而这在另一个控制器中:

class OtherController < ApplicationController

    def index
        example { some_text("irrelevant_text") }
    end

    private

    def some_text var
        "#{var}_string"
    end
end

I want to test that when calling the index action example method gets called with the irrelevant_text_string argument. 我想测试调用索引操作example方法时是否使用irrelevant_text_string参数调用了该方法。

Something like: 就像是:

describe 'index' do
    it 'should call example' do
        controller.should_receive(:example).with("irrelevant_text_string")
        get :index
    end
end

But this isn't working. 但这是行不通的。 I keep getting 我不断

received :example with unexpected arguments
expected: ("irrelevant_text_string")
          got: (no args)

Why does it say no args? 为什么不显示参数?
Any help will be appreciated 任何帮助将不胜感激

因为您要传递一个块,而不是一个参数。

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

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