我正在尝试使用 rspec 测试现有的 rails 项目。我想测试一个控制器,但遇到一个我无法解决的错误:S
这是我的规格代码;
require 'spec_helper'
describe BriefNotesController do
before(:all) do
@customer=Factory(:customer)
@project=Factory(:project_started, :owner => @customer)
end
context 'get :new' do
it 'should redirect to login page for not signed in users' do
get :new, :project_id => @project.id
response.should redirect_to("/kullanici-girisi")
end
it 'should be success and render new brief note page for project owner' do
sign_in @customer
get :new, :project_id => @project.id
response.should be_success
end
end
end
这是我的控制器代码;
class BriefNotesController < ApplicationController
before_filter :authenticate_user!
before_filter :find_project
def new
@brief_note = @project.brief_notes.new
end
def create
@brief_note = @project.brief_notes.build(params[:brief_note])
if @brief_note.save
redirect_to brief_project_path(@project)
else
render :action => :new
end
end
private
def find_project
@project = current_user.projects.find_by_cached_slug([params[:project_id]])
end
end
我认为 current_user.projects.find_by_cached_slug 方法不起作用。所以这是错误;
Failures:
1) BriefNotesController get :new should be success and render new brief note page for project owner
Failure/Error: get :new, :project_id => @project.id
NoMethodError:
undefined method `brief_notes' for nil:NilClass
# ./app/controllers/brief_notes_controller.rb:6:in `new'
# ./spec/controllers/brief_notes_controller_spec.rb:19:in `block (3 levels) in <top (required)>'