我对使用 Rails 进行测试非常陌生,并且似乎让自己迷失在测试的世界中。我目前正在测试我的仪表板控制器,当我从控制器中删除load_and_authorize_resource行时,一切都通过了。我正在使用 cancan 进行授权。
仪表板控制器.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
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
我从控制台收到的错误
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)>'
无论如何绕过这个错误而不删除我的dashboard_controller中的load_and_authorize_resource?