Reputation: 2380
For some reason my controller specs for create action don't work. What did I miss?
factories
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
error for 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
errors for 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]
Upvotes: 0
Views: 761
Reputation: 29389
In profiles_controller_spec
, you have a before_each
which is creating the user before your first example executes, so an additional create is not going to bump the profile count. Note that placing a before_each
after an example (it
) does not change the fact that all before_each
blocks at a given level are executed before any of the contained examples.'
As for the products_controller_spec
failures, you would need to show more of the code (e.g. relevant variables and before
blocks in containing blocks, code under test, etc.).
Upvotes: 1