我正在 Rspec2,Rails 3 中进行控制器测试,基本上我在通信控制器中有一个编辑操作,所以如果有人想编辑批量电子邮件,他们可以在草稿模式下。如果它已被批准或发送它不能被编辑。
我的控制器看起来像这样
class CommunicateController < ApplicationController
before_filter :setup
require_role 'view_communication'
#require_role 'edit_communication', :only => [:new, :edit, :create, :update, :destroy] Role not implemented
require_role 'approve_communication', :only => :approve
require_role 'send_communication', :only => :distribute
...
# GET /communications/1/edit
def edit
if @communication.status == 'Sent' || @communication.status == 'Approved'
flash[:error] = 'Cannot edit communication once it has been either approved or sent.'
redirect_to(communicate_path(@communication))
end
end
...
private
def setup
@communication = current_account.communications.find(params[:id]) if params[:id]
end
end
现在它可以在生产中使用,一切都很酷。现在的问题是我必须对其进行测试。
我的测试如下所示:
describe CommunicateController, "#edit" do
context "when i am editing a pre approved bulk email" do
before(:each) do
@account = FactoryGirl.create(:account)
@user = FactoryGirl.create(:superuser, :accounts => [@account])
controller.stub(:current_account).and_return(@account)
@supplier = FactoryGirl.create(:supplier, :account => @account)
@other_supplier = FactoryGirl.create(:supplier, :account => @account)
@supplier_report = FactoryGirl.create(:supplier_report, :reportable => @account)
@communicate = FactoryGirl.create(:communication, :account => @account, :supplier_report => @supplier_report)
controller.stub(:setup).and_return(@communicate)
end
it "denies edit access if the bulk email is sent" do
@communicate.status = 'Sent'
get :edit, :id => @communicate.id
response.should redirect_to(communicate_path(@communicate))
end
it "denies edit access if the bulk email has been approved" do
@communicate.status = 'Approved'
get :edit, :id => @communicate.id
response.should redirect_to(communicate_path(@communicate))
end
it "allows edits to Draft bulk email" do
@communicate.status = 'Draft'
get :edit, :id => @communicate.id
response.should redirect_to(edit_communicate_path)
end
end
end
正如您所看到的,为这个特定的测试设置环境非常复杂,需要满足所有要求。但是当我运行测试时,它给了我以下错误:
Failure/Error: response.should redirect_to(communicate_path(@communicate))
Expected response to be a redirect to <http://test.host/communicate/1> but was a redirect to <http://test.host/>
但对于所有三个测试,相同的错误。所以代码正在工作并且测试似乎正在寻找正确的路线,我只是不知道它为什么要寻找根路线。
抱歉,如果这是一个简单的错误,我只在 Rails 中编程了几个月,这实际上是我在 rspec 中的第一次控制器测试,所以我不确定要寻找什么,或者我的测试是否状况良好。
任何帮助或指导将不胜感激