我有一个“Mastertag”模型作为“Project”的嵌套资源,其创建操作如下:
def create
@mastertag = @project.mastertags.build(params[:mastertag])
if @mastertag.save
redirect_to project_mastertags_path, notice: 'Mastertag was successfully created.'
else
render action: "new"
end
end
其中@project 在过滤器之前的方法中初始化。
我有一个 rspec 测试:
describe "POST create" do
context "with valid params" do
it "creates a new Mastertag" do
expect {
post :create, { project_id: @project.id, mastertag: FactoryGirl.attributes_for(:mastertag_without_project) }
}.to change(Mastertag, :count).by(1)
end
end
当我运行测试时,@mastertag.save 方法返回 true,但计数仍然保持不变。测试因此失败。这看起来很奇怪。我哪里错了?