0

我在我的 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

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

编辑:我的控制器代码

  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)
4

1 回答 1

1

代替

@show = FactoryGirl.build(:show)

和:

@show = FactoryGirl.attributes_for(:show)
于 2012-07-30T15:14:41.193 回答