将参数传递给 post :创建请求 ruby-on-rails-3.1,Rspec,factory-girl
我正在尝试编写一个控制器规范来创建带有采购行项目的采购。使用我提供的所有属性都可以很好地创建购买,但不会创建购买行项目。这是我的代码:
factories.rb
FactoryGirl.define do
factory :purchase do |f|
f.sequence(:po_number) { |n| "0000-00#{n}" }
f.sequence(:ship_to) { |n| "Test Corp#{n}"}
f.sequence(:ship_via) {|n| "Test Delivery#{n}" }
f.sequence(:terms) {|n| "My terms#{n}" }
f.sequence(:qa_requirements) {|n| "Requirment#{n}" }
f.sequence(:justification) {|n| "Justification#{n}" }
f.become_part_of_delivered_product true
f.immediately_delivered_to_stc_client false
f.remain_for_contract_at_stc_as_gov_prop false
f.consumed_in_job_requirements true
f.used_in_repair_of_gov_prop false
f.is_gov_prop_in_possession_of_stc false
f.sequence(:required) {|n| "2011-10-0#{n}" }
f.prd_number_id { |n| n.association(:prd_number).id }
# f.order_for_id { |o| o.association(:user) }
f.submitted_by_id { |s| s.association(:submitted_by).id }
# f.ordered_for_date { Time.now.to_s }
f.submitted_by_date { Time.now.to_s }
f.quality_system_classification_id { |q| q.association(:quality_system_classification).id }
f.after_create { |c| Factory(:purchase_line_item, purchase: c) }
end
factory :purchase_line_item do |f|
f.sequence(:item_description) {|n| "Desc#{n}" }
f.unit_price "100.00"
f.qty "40"
f.sequence(:charge_number) {|n| "000-00#{n}" }
end
end
PurchaseLineItem.rb
belongs_to :purchase
validates_presence_of :item_description,
:unit_price,
:qty,
:charge_number
Purchase.rb
belongs_to :authorized_signers
belongs_to :vendor
belongs_to :purchase_type
belongs_to :quality_system_classfication
belongs_to :order_by, :class_name => "User", :foreign_key => "order_by_id"
belongs_to :submitted_by, :class_name => "User", :foreign_key => "submitted_by_id"
belongs_to :approved_by, :class_name => "User", :foreign_key => "approved_by_id"
belongs_to :purchased_by, :class_name => "User", :foreign_key => "purchased_by_id"
has_many :purchase_line_items
has_many :audits
accepts_nested_attributes_for :purchase_line_items, :allow_destroy => true
validates_presence_of :required,
:ship_to,
:ship_via,
:terms,
:quality_system_classification_id,
:prd_number_id
Purchase Controller
load_and_authorize_resource
def new
@purchase = Purchase.new
1.times { @purchase.purchase_line_items.build }
end
def create
@purchase = Purchase.new(params[:purchase])
@purchase.without_auditing do
if @purchase.save
flash[:notice] = "Successfully created purchase."
redirect_to @purchase
else
render action: 'new'
end
end
end
PurchaseController 规范
require 'spec_helper'
describe PurchasesController do
login_user
describe "POST create" do
before(:each) do
@ability.can :create, Purchase
end
it "should pass the params to purchase" do
purchase = Factory(:purchase)
post :create, purchase: purchase.attributes.except("id")
assigns(:purchase).po_number.should == purchase.po_number
end
it "should pass the params to purchase_line_items" do
purchase = Factory(:purchase)
post :create, purchase: purchase.attributes, purchase_line_items_attributes: purchase.purchase_line_items.first.attributes
assigns(:purchase).purchase_line_items.first.unit_price.should == purchase.unit_price
end
end
end
提前致谢
I am trying to write a controller spec for creating a purchase with a purchase line item. The purchase gets created just fine with all the attributes I give it but the purchase line item is not created. Here is my code:
factories.rb
FactoryGirl.define do
factory :purchase do |f|
f.sequence(:po_number) { |n| "0000-00#{n}" }
f.sequence(:ship_to) { |n| "Test Corp#{n}"}
f.sequence(:ship_via) {|n| "Test Delivery#{n}" }
f.sequence(:terms) {|n| "My terms#{n}" }
f.sequence(:qa_requirements) {|n| "Requirment#{n}" }
f.sequence(:justification) {|n| "Justification#{n}" }
f.become_part_of_delivered_product true
f.immediately_delivered_to_stc_client false
f.remain_for_contract_at_stc_as_gov_prop false
f.consumed_in_job_requirements true
f.used_in_repair_of_gov_prop false
f.is_gov_prop_in_possession_of_stc false
f.sequence(:required) {|n| "2011-10-0#{n}" }
f.prd_number_id { |n| n.association(:prd_number).id }
# f.order_for_id { |o| o.association(:user) }
f.submitted_by_id { |s| s.association(:submitted_by).id }
# f.ordered_for_date { Time.now.to_s }
f.submitted_by_date { Time.now.to_s }
f.quality_system_classification_id { |q| q.association(:quality_system_classification).id }
f.after_create { |c| Factory(:purchase_line_item, purchase: c) }
end
factory :purchase_line_item do |f|
f.sequence(:item_description) {|n| "Desc#{n}" }
f.unit_price "100.00"
f.qty "40"
f.sequence(:charge_number) {|n| "000-00#{n}" }
end
end
PurchaseLineItem.rb
belongs_to :purchase
validates_presence_of :item_description,
:unit_price,
:qty,
:charge_number
Purchase.rb
belongs_to :authorized_signers
belongs_to :vendor
belongs_to :purchase_type
belongs_to :quality_system_classfication
belongs_to :order_by, :class_name => "User", :foreign_key => "order_by_id"
belongs_to :submitted_by, :class_name => "User", :foreign_key => "submitted_by_id"
belongs_to :approved_by, :class_name => "User", :foreign_key => "approved_by_id"
belongs_to :purchased_by, :class_name => "User", :foreign_key => "purchased_by_id"
has_many :purchase_line_items
has_many :audits
accepts_nested_attributes_for :purchase_line_items, :allow_destroy => true
validates_presence_of :required,
:ship_to,
:ship_via,
:terms,
:quality_system_classification_id,
:prd_number_id
Purchase Controller
load_and_authorize_resource
def new
@purchase = Purchase.new
1.times { @purchase.purchase_line_items.build }
end
def create
@purchase = Purchase.new(params[:purchase])
@purchase.without_auditing do
if @purchase.save
flash[:notice] = "Successfully created purchase."
redirect_to @purchase
else
render action: 'new'
end
end
end
PurchaseController spec
require 'spec_helper'
describe PurchasesController do
login_user
describe "POST create" do
before(:each) do
@ability.can :create, Purchase
end
it "should pass the params to purchase" do
purchase = Factory(:purchase)
post :create, purchase: purchase.attributes.except("id")
assigns(:purchase).po_number.should == purchase.po_number
end
it "should pass the params to purchase_line_items" do
purchase = Factory(:purchase)
post :create, purchase: purchase.attributes, purchase_line_items_attributes: purchase.purchase_line_items.first.attributes
assigns(:purchase).purchase_line_items.first.unit_price.should == purchase.unit_price
end
end
end
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Rails 4 下,这种通用性对我不起作用:
以下是我发现的通用样式,可以满足 Rails 4 的帖子:create ...updated。 注意:valid_session 可能无法完全按照我的存根工作,但是您明白 DRY 的想法;)
在 Rails 4 中,如果您设置控制器以利用:
并从模型中删除
attr_accessible
,那么您不必担心使用attributes. except(...)
进行过滤。Under Rails 4, this generality did not work for me:
The following is a generalized style that I discovered will satisfy the post :create ...updated for Rails 4. Note: The valid_session may not work exactly as I am stubbing, but you get the DRY idea ;)
In Rails 4, if you set up the controller to utilize:
and removing the
attr_accessible
from the models, then you don't have to worry about using theattributes.except(...)
to filter.想通了。我改变了一些东西,但问题是语法。在PurchasesController 规范中,我将代码更改为:
语法需要从: 更改
为:
现在一切正常!
Figured it out. I change some things around but the problem was syntax. In the PurchasesController Spec I changed the code to:
The syntax needed to be changed from:
to:
Now everything works!