Rspec POST控制器创建测试失败,但基于Web的提交工作

时间:2012-07-30 13:13:39

标签: ruby-on-rails ruby rspec

我在

中对create方法进行了以下rspec测试
describe "with valid information" do
  it "should respond with success" do
    post 'create', :show_secretary_id => @show_secretary.id, :show => @show
    response.should be_success
  end

  it "should incremenet the show count" do
    expect do 
      post 'create', :show_secretary_id => @show_secretary.id, :show => @show
    end.to change(Show,'count').by(1)
  end
end

测试失败。但是,当我在浏览器中尝试创建方法时,它可以工作。关于我缺少的任何想法?

编辑:我的控制器代码

  def create
    @show_secretary = ShowSecretary.find_by_id(params[:show_secretary_id])
    @show = @show_secretary.shows.build(params[:show])
    if @show.save
      flash[:notice] = "Successfully created show"
      redirect_to show_path @show 
    else
      render 'new'
    end
  end
编辑:@show_secretary,@ show

这两个对象分别是由FactoryGirl创建和构建的ActiveRecords。

@show_secretary = FactoryGirl.create(:show_secretary_user).verifiable
@show = FactoryGirl.build(:show)

1 个答案:

答案 0 :(得分:1)

替换

@show = FactoryGirl.build(:show)

with:

@show = FactoryGirl.attributes_for(:show)