我对测试和rails非常陌生,并试图自己解决这个问题,但没有运气.
我有以下型号
- class Picture < ActiveRecord::Base
- belongs_to :product
- has_attached_file :image
- end
- class Product < ActiveRecord::Base
- has_many :pictures,:dependent => :destroy
- accepts_nested_attributes_for :pictures,:reject_if => lambda { |p| p[:image].blank? },:allow_destroy => true
- end
和一个相当标准的控制器,我猜……
- def create
- @product = Product.new(params[:product])
- if @product.save
- redirect_to products_path,:notice => "blah."
- else
- render :action => "new"
- end
- end
我将如何进行测试呢?我试过这样的东西,但是我无法让它起作用:
- describe ProductsController do
- it "adds given pictures to the product" do
- product = Factory.build(:product)
- product.pictures.build(Factory.attributes_for(:picture))
- post :create,:product => product.attributes
- Product.where(:name => product[:name]).first.pictures.count.should == 1 # or something
- end
- end
它可能与属性传递给create action的方式有关,但我怎样才能使它工作?我使用rails 3.1.rc5,但我怀疑这与它为什么不工作有任何关系……
或者你不会测试这个,因为它是基本的轨道功能,并且最有可能在开始时经过良好测试?