Я развиваюсь в RoR уже более года, но я только начинаю использовать тесты, используя RSpec.
Для стандартных тестов модели/контроллера у меня обычно нет проблем, но проблема в том, что я хочу протестировать некоторые сложные функциональные процессы и не знаю, как структурировать свои тестовые папки/файлы/базу данных.
Вот базовая структура для моего приложения:
class Customer
has_one :wallet
has_many :orders
has_many :invoices, through: :orders
has_many :invoice_summaries
end
class Wallet
belongs_to :customer
end
class Order
has_one :invoice
belongs_to :customer
end
class Invoice
belongs_to :order
belongs_to :invoice_summary
end
class InvoiceSummary
belongs_to :customer
has_many :invoices
end
Основная проблема заключается в том, что я хочу моделировать жизненный цикл моего объекта, что означает:
-
Создание клиентов и кошельков, которые будут использоваться для всех тестов (без повторной инициализации)
-
Имитация потока времени, создание и обновление нескольких объектов заказов/счетов и некоторых счетов-фактур.
Для создания и обновления заказов/счетов/invoice_summaries мне бы хотелось иметь такие методы, как
def create_order_1
# code specific to create my first order, return the created order
end
def create_order_2
# code specific to create my second order, return the created order
end
.
.
.
def create_order_n
# code specific to create my n-th order, return the created order
end
def bill_order(order_to_bill)
# generic code to do the billing of the order passed as parameter
end
def cancel_order(order_to_cancel)
# generic code to cancel the order passed as parameter
end
Ive уже нашел драгоценный камень Timecop для моделирования потока времени. Следовательно, я хотел бы иметь понятный окончательный тест, который выглядит как
# Code for the initialization of customers and wallets object
describe "Wallet should be equal to 0 after first day" do
Timecop.freeze(Time.new(2015,7,1))
first_request = create_request_1
first_request.customer.wallet.value.should? == 0
end
describe "Wallet should be equal to -30 after second day" do
Timecop.freeze(Time.new(2015,7,2))
bill_order(first_request)
second_order = create_order_2
first_request.customer.wallet.value.should? == -30
end
describe "Wallet should be equal to -20 after third day" do
Timecop.freeze(Time.new(2015,7,3))
bill_order(second_request)
cancel_order(first_request)
first_request.customer.wallet.value.should? == -20
end
describe "Three first day invoice_summary should have 3 invoices" do
Timecop.freeze(Time.new(2015,7,4))
invoice_summary = InvoiceSummary.create(
begin_date: Date.new(2015,7,1),
end_date: Date.new(2015, 7,3)
) # real InvoiceSummary method
invoice_summary.invoices.count.should? == 3
end
У кого-нибудь уже есть такие тесты? Существуют ли хорошие методы структурирования объектов-объектов, написания тестов и т.д.?
Например, мне сказали, что хорошей идеей было бы создать создание Customer/Wallet в файле db/seed.rb, но я действительно не знаю, что с ним делать потом.