У меня есть следующее испытание. Есть три блока it
. Первый не использует shoulda
в отличие от двух других.rspec + shoulda: настройка данных
Если я не использую блок before
с номером post :create, product: attrs
, то первое испытание завершится неудачно, как ожидалось. Но если я поставлю блок before
, тогда первый тест не удастся, но остальные два пройдут. У меня есть проверка уникальности имени продукта, но это не должно быть проблемой, поскольку я использую последовательность с фабрикой.
Что мне делать? Как я обычно настраиваю данные для тестирования, когда одновременно присутствуют контроллеры rspec и shoulda?
describe "when user logged in" do
before(:each) do
login_user #logged in user is available by calling @user
end
context "POST create" do
context "with valid attributes" do
let!(:profile) { create(:profile, user: @user) }
let!(:industry) { create(:industry) }
let!(:attrs) { attributes_for(:product, user_id: @user.id, industry_ids: [ industry.id ]).merge(
product_features_attributes: [attributes_for(:product_feature)],
product_competitions_attributes: [attributes_for(:product_competition)],
product_usecases_attributes: [attributes_for(:product_usecase)]
) }
it "saves the new product in the db" do
expect{ post :create, product: attrs }.to change{ Product.count }.by(1)
end
#If I don't use this the 2 tests below fail. If I use it, then the test above fails.
# before do
# post :create, product: attrs
# end
it { is_expected.to redirect_to product_path(Product.last) }
it { is_expected.to set_flash.to('Product got created!') }
end
end
end
фабрики
factory :product, class: Product do
#name { Faker::Commerce.product_name }
sequence(:name) { |n| "ABC_#{n}" }
company { Faker::Company.name }
website { 'https://example.com' }
oneliner { Faker::Lorem.sentence }
description { Faker::Lorem.paragraph }
user
end
Вы считали, что используете различные блоки контекста? Это позволит вам иметь отдельный блок перед блоками. –
Петр, я сделал. Я просто не знаю, как правильно это сделать. В верхней части этого я хотел бы понять корень проблемы. –