我已经在互联网上搜索了很多,以及#2等类似的问题使用RSpec + FactoryGirl嵌套资源的作用,但我仍然不知道如何测试一个嵌套资源的创建方法在我的rails应用程序中。测试“创建”
资源路线
resources :projects, :except => [:index, :show] do
resources :mastertags
end
这里是动作我想测试:
def create
@mastertag = @project.mastertags.build(params[:mastertag])
respond_to do |format|
if @mastertag.save
format.html { redirect_to project_mastertags_path, notice: 'Mastertag was successfully created.' }
else
format.html { render action: "new" }
end
end
end
这是我与Rspec的测试:
context "with valid params" do
it "creates a new Mastertag" do
project = Project.create! valid_attributes[:project]
mastertag = Mastertag.create! valid_attributes[:mastertag]
expect {
post :create, { project_id: project.id, :mastertag => valid_attributes[:mastertag] }
}.to change(Mastertag, :count).by(1)
end
end
我有一个valid_attributes功能:
def valid_attributes
{ :project => FactoryGirl.attributes_for(:project_with_researcher), :mastertag => FactoryGirl.attributes_for(:mastertag) }
end
我得到以下错误:
Failure/Error: post :create, { project_id: project.id, :mastertag => valid_attributes[:mastertag] }
NoMethodError:
undefined method `reflect_on_association' for "5168164534b26179f30000a1":String
我也试过一对夫妇的变化,但似乎没有任何工作。