Rspec:如何在控制器规范中分配实例变量

ole*_*ole 40 rspec ruby-on-rails

class TestController < AplicationController
  #....

  private

  def some_method
    unless @my_variable.nil?
      #...
      return true
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我想some_method直接在控制器规范中测试:

require 'spec_helper'

describe TestController do
  it "test some_method"
    phone = Phone.new(...)
    controller.assign(:my_variable,phone) #does not work
    controller.send(:some_method).should be_true
  end
end
Run Code Online (Sandbox Code Playgroud)

如何从控制器规范中设置TestController实例变量@my_variable

Pau*_*nti 60

当在控制器中测试私有方法而不是使用时send,我倾向于使用匿名控制器,因为不想直接调用私有方法,而是私有方法的接口(或者,在下面的测试中,有效地存在该接口).所以,在你的情况下,可能是这样的:

require 'spec_helper'

describe TestController do
  controller do
    def test_some_method
      some_method
    end
  end

  describe "a phone test with some_method" do

    subject { controller.test_some_method }

    context "when my_variable is not nil" do
      before { controller.instance_variable_set(:@my_variable, Phone.new(...)) }
      it { should be_true }
    end

    context "when my_variable is nil" do
      before { controller.instance_variable_set(:@my_variable, nil) } 
      it { should_not be_true } # or should be_false or whatever
    end     
  end
end
Run Code Online (Sandbox Code Playgroud)

关于在StackOverflow问答中直接测试私有方法的问题有一些很好的讨论,这使我对使用匿名控制器产生了影响,但您的意见可能有所不同.


ReW*_*ite 5

instance_eval是实现此目的的一种相对干净的方法:

describe TestController do
  it "test some_method" do
    phone = Phone.new(...)
    controller.instance_eval do
      @my_variable = phone
    end
    controller.send(:some_method).should be_true
  end
end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,使用do...endoninstance_eval就太过分了,这三行可以缩短为:

controller.instance_eval {@my_variable = phone}
Run Code Online (Sandbox Code Playgroud)