与我的工厂一起使用attributes_for时RSpec测试中的错误

时间:2014-03-31 13:18:12

标签: ruby-on-rails rspec

我有以下工厂:

FactoryGirl.define do
  factory :category do
    name "Example  category"
    user_id 1
  end
end

这是我的测试:

describe "POST #create" do
  context "signed in" do
    before(:each) do
      sign_in :user, user
    end

    context "with valid attributes" do
      it "saves the new category in the database" do
        expect{
          post :create, FactoryGirl.attributes_for(:category)
        }.to change(Category, :count).by(1)
      end

      it "redirects to category #index"
    end

    context "with invalid attributes" do
      it "does not save the new category in the database"
      it "re-renders the :new template"
      it "provides errors"
    end
  end

  context "not signed in" do
    it "blocks access (somehow)"
  end
end

但是,当我运行测试时,我收到以下错误:

3) CategoriesController POST #create signed in with valid attributes saves the new category in the database
  Failure/Error: post :create, FactoryGirl.attributes_for(:category)
  ActionController::ParameterMissing:
    param not found: category

我之前设法使用了工厂,它在我的规格/型号/测试中运行良好。我在这里做错了什么吗?我看不出工厂有任何问题。我有点失落:(

1 个答案:

答案 0 :(得分:0)

您必须提供密钥:

post :create, category: FactoryGirl.attributes_for(:category)