Ruby on rails Rspec在控制器中更新失败
以下是客户控制器中更新的rspec错误:Ruby on rails Rspec在控制器中更新失败,ruby-on-rails,ruby-on-rails-3.1,rspec2,Ruby On Rails,Ruby On Rails 3.1,Rspec2,以下是客户控制器中更新的rspec错误: 5) CustomersController GET customer page 'update' should be successful Failure/Error: put 'update', id => 1, :customer => {:name => 'name changed'} <Customer(id: integer, name: string, short_name: string,
5) CustomersController GET customer page 'update' should be successful
Failure/Error: put 'update', id => 1, :customer => {:name => 'name changed'}
<Customer(id: integer, name: string, short_name: string, contact: string, address: string, country: string, phone: string, fax: string, email: string, cell:
string, sales_id: integer, test_eng_id: integer, safety_eng_id: integer, web: string, category1_id: integer, category2_id: integer, active: boolean, biz_status: str
ing, input_by_id: integer, quality_system: string, employee_num: string, revenue: string, note: text, user_id: integer, created_at: datetime, updated_at: datetime)
(class)> received :find with unexpected arguments
expected: (1)
got: ("1")
# ./app/controllers/customers_controller.rb:44:in `update'
# ./spec/controllers/customers_controller_spec.rb:41:in `block (3 levels) in <top (required)>'
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(params[:customer], :as => :roles_new_update)
if @customer.changed
@message = 'The following info have been changed\n' + @customer.changes.to_s
@subject ='Customer info was changed BY' + session[:user_name]
notify_all_in_sales_eng(@message,@subject)
end
redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!'
else
render 'edit', :notice => 'Customer was not updated!'
end
end
以下是控制器中的更新:
5) CustomersController GET customer page 'update' should be successful
Failure/Error: put 'update', id => 1, :customer => {:name => 'name changed'}
<Customer(id: integer, name: string, short_name: string, contact: string, address: string, country: string, phone: string, fax: string, email: string, cell:
string, sales_id: integer, test_eng_id: integer, safety_eng_id: integer, web: string, category1_id: integer, category2_id: integer, active: boolean, biz_status: str
ing, input_by_id: integer, quality_system: string, employee_num: string, revenue: string, note: text, user_id: integer, created_at: datetime, updated_at: datetime)
(class)> received :find with unexpected arguments
expected: (1)
got: ("1")
# ./app/controllers/customers_controller.rb:44:in `update'
# ./spec/controllers/customers_controller_spec.rb:41:in `block (3 levels) in <top (required)>'
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(params[:customer], :as => :roles_new_update)
if @customer.changed
@message = 'The following info have been changed\n' + @customer.changes.to_s
@subject ='Customer info was changed BY' + session[:user_name]
notify_all_in_sales_eng(@message,@subject)
end
redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!'
else
render 'edit', :notice => 'Customer was not updated!'
end
end
有什么想法吗?谢谢。发布的值(通过参数访问)是字符串。纠正测试的最简单方法是让客户。应该接收(:find)。使用(“1”)。并返回(客户)
请注意,我们现在使用“1”(即字符串)作为预期参数,而不是1(FixNum)。所有参数都作为字符串传递,我相信find方法正在对整数进行隐式对话。要么在控制器中显式使用to_i,要么更改您的规范以期望字符串“1”。这是与(“1”)一起使用后的错误:5)CustomerController获取客户页面“更新”应成功失败/错误:put'update',:id=>1,:customer=>{:name=>'name changed'}Mock“customer_1001”收到意外消息:更改为(无参数)#./app/controllers/customers\u controller.rb:46:inupdate'./spec/controllers/customers\u controller\u spec.rb:41:in
block(3层)in'这是因为您正在调用@customer.changed
,但尚未中断该方法。您需要存根该方法,或者使用真实的模型实例,以便@customer.changed
能够正确计算。是的,您是对的。存根类似于:customer.stub(:changed.),并且返回(false)以绕过发送电子邮件。但是遇到了另一个问题,那就是如何测试:重定向到会话[('page'+会话[:page\u step].到_s).到_sym],:notice=>“客户已成功更新!”?另一个问题。通过使用存根按需返回值,我们真的测试了代码中的任何内容吗?或者只是为了自我安慰而让它通过。你应该能够用响应测试这一点。应该重定向到(会话[('page'+会话[:page\u步骤])。到。到sym])
。rspec测试的工作方式是在每个测试中只测试一件事情。其他所有内容都是存根的,因此每个测试都是独立的。因此,您应该使用另一个测试来检查@customer.changed
是否正常工作。这样你就有了适当的测试覆盖率。