由於某些原因,我的控制器規範創建動作不起作用。我錯過了什麼?用於與工廠創建動作的rspec控制器規範
工廠
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調用,楷模。通常情況下,這會讓人難以診斷問題。 –