gpt4 book ai didi

ruby - 在测试失败的 View 中使用 CanCanCan

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:08 24 4
gpt4 key购买 nike

你好,我有一个能力的项目:

 can :set_to_user, Post

然后我有一个观点:

<div class: "btn btn-primary #{disable_button(!(can_set_to_user))}>

在我的助手中:

def can_set_to_user 能? :set_to_user, 发布 结束

当我运行服务器时这工作正常,但是当我运行我的测试时它们似乎崩溃并给出这个错误?

ActionView::Template::Error:
Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.

我不确定是什么原因造成的,但任何帮助都会很棒

最佳答案

Warden::Proxy 用于 current_useruser_signed_in? 方法。您必须在 View 规范中 stub 这些方法才能使其工作:

let(:user) { FactoryGirl.create :user }

before(:example) do
allow(view).to receive(:current_user).and_return(user)
allow(view).to receive(:user_signed_in?).and_return(!!user)
end

参见 devise source

关于ruby - 在测试失败的 View 中使用 CanCanCan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40243366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com