rspec控制器规范用于使用工厂创建操作

时间:2016-04-24 13:44:28

标签: ruby-on-rails rspec controller rspec-rails

出于某种原因,我的控制器规范创建操作不起作用。我错过了什么?

工厂

factory :profile do
  first_name { "John" }
  last_name { "Doe" }
  job_title { Faker::Name.title }
  company { "Faskyn" }
  avatar { Faker::Avatar.image }
  location { Faker::Address.city }
  description { Faker::Lorem.sentence }
  phone_number { Faker::PhoneNumber.cell_phone }
  user
end

factory :product, class: Product do
  name { Faker::Commerce.product_name }
  company { Faker::Company.name }
  website { 'https://example.com' }
  oneliner { Faker::Lorem.sentence }
  description { Faker::Lorem.paragraph }
  user
  trait :product_with_nested_attrs do
    before(:create) do |product|
      product.product_competitions << build(:product_competition, product: product)
      product.product_usecases << build(:product_usecase, product: product)
      product.product_features << build(:product_feature, product: product)
      product.industries << build(:industry)
    end
  end
end 

profiles_controller_spec

describe "POST create" do
  before(:each) do
    login_user
  end
  context "with valid attributes" do

    it "saves the new profile in the db" do
      expect{ post :create, user_id: @user.id, profile: attributes_for(:profile, user: @user) }.to change(Profile, :count).by(1)
    end

    before(:each) do
      post :create, user_id: @user.id, profile: attributes_for(:profile, user: @user)
    end

    it { is_expected.to redirect_to add_socials_user_profile_path(@user) }
  end
end
profiles_controller_spec

的错误

ProfilesController POST create with valid attributes saves the new profile in the db
 Failure/Error: expect{ post :create, user_id: @user.id, profile: attributes_for(:profile, user: @user) }.to change(Profile, :count).by(1)
   expected #count to have changed by 1, but was changed by 0

products_controller_spec

context "POST create" do
  context "with valid attributes" do

    it "saves the new product in the db" do
      product_attrs = attributes_for(:product, :product_with_nested_attrs, user: @user)
      expect{ post :create, product: product_attrs }.to change{ Product.count }.by(1)
    end

    it { is_expected.to redirect_to product_path(Product.last) }
  end
end

products_controller_spec的错误

ProductsController when user is logged in POST create with valid attributes saves the new product in the db
 Failure/Error: expect{ post :create, product: product_attrs }.to change{ Product.count }.by(1)
   expected result to have changed by 1, but was changed by 0

ProductsController when user is logged in POST create with valid attributes
 Failure/Error: it { is_expected.to redirect_to product_path(Product.last) }

 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"products", :id=>nil} missing required keys: [:id]

1 个答案:

答案 0 :(得分:1)

profiles_controller_spec中,您有before_each在第一个示例执行之前创建用户,因此额外的创建不会影响配置文件计数。请注意,在示例(before_each)之后放置it并不会改变在任何包含的示例之前执行给定级别的所有before_each块的事实。'

对于products_controller_spec失败,您需要显示更多代码(例如,包含块中的相关变量和before块,正在测试的代码等。)