In a webshop application normally after a booking of a product the bookings_controller create action does an order.save
which in turn activates the necessary before_save method order.sum_of_all_bookings
.
When building a RSpec test for an admin viewing a list of orders, making a booking doesn't change the total of an order. The indexing does work in the browser.
require 'rails_helper'
RSpec.feature 'Admin can oversee orders' do
let(:admin) { FactoryGirl.create(:user, :admin) }
let(:customer) { FactoryGirl.create( :user ) }
let!(:order_1) { FactoryGirl.create( :order, customer: customer ) }
let!(:booking_1) { FactoryGirl.create( :booking, product_name: 'Honingpot',
product_quantity: 1,
product_price: '5,00',
order: order_1 ) }
let!(:order_2) { FactoryGirl.create( :order, customer: customer ) }
let!(:booking_2) { FactoryGirl.create( :booking, product_name: 'Streekpakket',
product_quantity: 2,
product_price: '10,00',
order: order_2 ) }
before do
order_1.sum_all_bookings
order_2.sum_all_bookings
login_as(admin)
visit orders_path
end
scenario 'with success' do
within('table#paid') do
expect(page).to have_content '5,00'
expect(page).to have_content '10,00'
end
end
end
Before block doesn't work.
Neither does the factory girl after create:
FactoryGirl.define do
factory :booking do
product_name 'Honingpot'
product_quantity '1'
product_price '3,99'
order
after(:create) { |booking| booking.order.save } # booking.order.sum_all_bookings, also not working
end
end
RSpec prints the bookings being in the order, yet the order total being unchanged.
How to make the order sum the bookings?
Or more generally:
How to make a FactoryGirl.create
affect another record's attribute?