【问题标题】:rspec controller spec for create action with factoryrspec 控制器规范,用于使用工厂创建操作
【发布时间】:2016-04-24 13:44:28
【问题描述】:

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

工厂

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]

【问题讨论】:

  • 你已经展示了你的工厂、你的规格和你的失败信息,但是你没有展示任何被测试的软件,包括你的路由、你的控制器,除非你在模拟ActiveRecord 调用,您的模型。通常,这会使某人难以诊断问题。

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


【解决方案1】:

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

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

【讨论】:

  • Peter,关于配置文件:我可以通过将 @user.profile.destroy 放在 saves the new profile in the db 测试中的期望之前来解决问题。我想这不是最好的方法。你能提供一些你会怎么做的代码吗?至于产品,我刚刚在这里发布了一个新问题:stackoverflow.com/questions/36827856/… 你也可以看看吗?我想问题是相似的。我真的不知道如何正确设置数据。如果你知道一个好的资源,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多