我已经安装了设备并且rspec设置已准备好使用controller_macros
我的ControllerMacro:
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@current_user = Fabricate(:user)
@current_user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
sign_in @current_user
end
end
和我的payment_controller_spec.rb
describe PaymentController do
describe "payment" do
login_user
before(:each) do
@current_user.payments.create(method: "bank", amount: "123")
end
it 'should assign amount_to_pay' do
get :bank_payment, { id: @current_user.to_param}
expect(assigns[:amount_to_pay]).to eq "123"
end
end
end
错误消息
NoMethodError: undefined method `payments' for nil:NilClass
如何在我的规范中访问@current_user
并添加一些依赖项?
谢谢
答案 0 :(得分:0)
我会在你的控制器宏中建议这样的事情:
def stub_accessibility(user)
controller.stub(:current_user).and_return(user)
end
然后在您的控制器规格中:
let(:user) { User.new }
before do
stub_accessibility(user)
end
通过这种方式,您可以在控制器规格的上下文中使用user
。