在rspec的轨道
问题描述:
我怎么能翻番@user
VAR方法中我怎样才能加倍的变量,如果我不加倍,测试将抛出的零的在rspec的轨道
呼叫update_attributes方法错误:NilClass
class TestController
def foo!
@user = current_user
@user.update_attribute(user_params)
end
end
RSpec.describe TestController, type: :controller do
describe "#foo" do
it "should be passed" do
@specific_user = FactoryGirl.create(:user)
allow_any_instance_of(TestController).to receive(:foo!).and_return(true)
allow_any_instance_of(TestController).to receive(@user).and_return(@specific_user)
end
end
end
答
你测试的问题是它没有测试任何东西。控制器测试应该对测试方法get :foo!
关于磕碰,你的情况current_user
方法的请求可以代替存根:
RSpec.describe TestController, type: :controller do
describe "#foo" do
it "should be passed" do
@specific_user = FactoryGirl.create(:user)
allow(controller).to receive(:foo!).and_return(true)
allow(controller).to receive(:current_user).and_return(@specific_user)
end
end
end
,是的,在控制器测试控制器实例可以通过访问调用controller
方法。
什么也允许在该控制器设置一个实例变量:
RSpec.describe TestController, type: :controller do
describe "#foo" do
it "should be passed" do
@specific_user = FactoryGirl.create(:user)
allow(controller).to receive(:foo!).and_return(true)
controller.instance_variable_set(:@user, @specific_user)
end
end
end