[英]Rspec Failure/Error testing devise and cancan
I am very new to testing with rails and have seemed to got myself lost in the world of testing. 我对使用Rails进行测试非常陌生,似乎迷失了测试领域。 I am currently testing my dashboard controller and everything passes when I remove the load_and_authorize_resource line from my controller. 我目前正在测试仪表板控制器,当我从控制器中删除load_and_authorize_resource行时,一切都会通过。 I am using cancan for authorization. 我正在使用cancan进行授权。
dashboard_controller.rb dashboard_controller.rb
def update
@dashboard = Dashboard.find(params[:id])
respond_to do |format|
if @dashboard.update_attributes(params[:dashboard])
format.html { redirect_to dashboards_path, notice: 'dashboard was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @dashboard.errors, status: :unprocessable_entity }
end
end
end
dashboard_controller_spec.rb dashboard_controller_spec.rb
require 'spec_helper'
describe DashboardsController do
login_user
before :each do
@dashboard = create(:dashboard, dashboard_name: "My Own Dashboard", id: 1)
end
describe "GET #index" do
it "should have a current_user" do
subject.current_user.should_not be_nil
end
it "renders the :index view" do
get :index
response.should render_template :index
end
it "Creates new dashboard" do
get :new
response.should render_template :new
end
end
describe "Get #edit" do
it "assigns dashboard to @dashboard" do
get :edit, id: @dashboard
assigns(:dashboard).should == @dashboard
end
it "renders the :edit template" do
get :edit, id: @dashboard
response.should render_template :edit
end
end
end
The error I receive from console 我从控制台收到的错误
1) DashboardsController Get #edit renders the :edit template
Failure/Error: response.should render_template :edit
expecting <"edit"> but rendering with <"">
# ./spec/controllers/dashboard_controller_spec.rb:37:in `block (3 levels) in <top (required)>'
Anyway to bypass this error without removing the load_and_authorize_resource in my dashboard_controller? 无论如何要在不删除我的dashboard_controller中的load_and_authorize_resource的情况下绕过此错误?
Testing controllers when Devise is involved is a annoyingly trickier. 涉及Devise时测试控制器非常烦人。 You don't provide your login_user code, but I'm guessing that it's not covering all the bases. 您没有提供您的login_user代码,但我猜测它没有涵盖所有基础。 As per the official Devise documentation , you need to help things along with warden, Devise's utility methods, etc. I'd repeat here, but it's a bit longer and you mine as well go to the source. 根据Devise的官方文档 ,您需要帮助其他人员,包括管理员,Devise的实用程序方法等。我在这里重复一遍,但是要花更长的时间,您也可以从源头获得。 The most pertinent part is: 最相关的部分是:
Controller specs won't work out of the box if you're using any of devise's utility methods. 如果您使用devise的任何实用程序方法,则控制器规范将无法立即使用。
As of rspec-rails-2.0.0 and devise-1.1, the best way to put devise in your specs is simply to add the following into spec_helper... 从rspec-rails-2.0.0和devise-1.1开始,将devise放入规格中的最佳方法就是将以下内容添加到spec_helper中...
Then add in their sample controller_macros.rb and tweak some other things and you should be good to go. 然后添加他们的示例controller_macros.rb并进行一些其他调整,您应该会做得很好。 However, do note that testing your controller specs are different from testing request specs and you might run into hangups in that different scenario that this solution doesn't address (see some of my own pain and discovery at https://stackoverflow.com/a/13275366/9344 ). 但是,请注意,测试控制器规格与测试请求规格不同,在此解决方案无法解决的不同情况下,您可能会遇到挂断(请参阅https://stackoverflow.com/上我的一些痛苦和发现) a / 13275366/9344 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.