【发布时间】:2014-05-13 09:04:11
【问题描述】:
有时新的(非常干燥的)rspec 语法让我抓狂...
Rspec v 2.14.1
describe "POST create" do
subject { post :create, contractor: valid_params }
context "by user" do
before { sign_in @legal.user }
it "contractor successful created" do
expect { subject }.to redirect_to(contractor_path(assigns(:contractor).id))
我在这里有错误和问题:
NoMethodError: # :contractor variable not defined
undefined method `id' for nil:NilClass
似乎expect 在控制器方法post 执行之前取了一个运算符,因为我试图引发这个方法。
我的代码:
def create
@contractor = Contractor.restrict!(current_accreditation).new(permitted_params) # TODO move to the IR::Base
if @contractor.save
current_accreditation = @contractor.create_legal!(user: current_user) # TODO legal create
redirect_to(@contractor)
else
render(:new)
end
end
其次,为什么我在尝试的时候会出错
expect(subject).to ...
为什么{} 有效,但() 无效?在津津乐道的文档中,这种方法效果很好:https://www.relishapp.com/rspec/rspec-rails/docs/matchers/redirect-to-matcher
【问题讨论】:
标签: ruby-on-rails ruby rspec rspec-rails