ruby-on-rails – 如何构建我的RSpec测试文件夹,文件和数据库?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何构建我的RSpec测试文件夹,文件和数据库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经在RoR开发了一年多了,但是我刚开始使用测试,使用RSpec.

对于标准模型/控制器测试,我通常没有任何问题,但问题是我想测试一些复杂的功能过程,并不真正知道如何构建我的测试文件夹/文件/数据库.

这是我的应用程序的基本结构:

  1. class Customer
  2. has_one :wallet
  3. has_many :orders
  4. has_many :invoices,through: :orders
  5. has_many :invoice_summaries
  6. end
  7.  
  8. class Wallet
  9. belongs_to :customer
  10. end
  11.  
  12. class Order
  13. has_one :invoice
  14. belongs_to :customer
  15. end
  16.  
  17. class Invoice
  18. belongs_to :order
  19. belongs_to :invoice_summary
  20. end
  21.  
  22. class InvoiceSummary
  23. belongs_to :customer
  24. has_many :invoices
  25. end

主要的问题是我想模拟我对象的生命周期,意思是:

>实例化用于所有测试的客户和钱包(无需重新初始化)
>模拟时间流程,创建和更新多个订单/发票对象和一些invoice_summaries.

对于订单/发票/ invoice_summaries的创建和更新,我想有一些方法

  1. def create_order_1
  2. # code specific to create my first order,return the created order
  3. end
  4.  
  5. def create_order_2
  6. # code specific to create my second order,return the created order
  7. end
  8. .
  9. .
  10. .
  11. def create_order_n
  12. # code specific to create my n-th order,return the created order
  13. end
  14.  
  15. def bill_order(order_to_bill)
  16. # generic code to do the billing of the order passed as parameter
  17. end
  18.  
  19. def cancel_order(order_to_cancel)
  20. # generic code to cancel the order passed as parameter
  21. end

我已经找到了用于模拟时间流的宝石Timecop.因此,我想要一个容易理解的最终测试

  1. # Code for the initialization of customers and wallets object
  2.  
  3. describe "Wallet should be equal to 0 after first day" do
  4. Timecop.freeze(Time.new(2015,7,1))
  5. first_request = create_request_1
  6. first_request.customer.wallet.value.should? == 0
  7. end
  8.  
  9. describe "Wallet should be equal to -30 after second day" do
  10. Timecop.freeze(Time.new(2015,2))
  11. bill_order(first_request)
  12. second_order = create_order_2
  13. first_request.customer.wallet.value.should? == -30
  14. end
  15.  
  16. describe "Wallet should be equal to -20 after third day" do
  17. Timecop.freeze(Time.new(2015,3))
  18. bill_order(second_request)
  19. cancel_order(first_request)
  20. first_request.customer.wallet.value.should? == -20
  21. end
  22.  
  23. describe "Three first day invoice_summary should have 3 invoices" do
  24. Timecop.freeze(Time.new(2015,4))
  25. invoice_summary = InvoiceSummary.create(
  26. begin_date: Date.new(2015,1),end_date: Date.new(2015,3)
  27. ) # real InvoiceSummary method
  28. invoice_summary.invoices.count.should? == 3
  29. end

有没有人有这样的测试?对象工厂的结构,写作测试等都有很好的做法吗?

例如,我被告知,一个好主意是将客户/电子钱包创建放在一个db / seed.rb文件中,但我不知道该怎么做.

解决方法

完全回答你的问题可以填写并填写书籍,所以我只能在这里概述答案.

关于创建要测试的对象,

> db / seeds.rb不适用于测试数据,但不适用于应用程序运行,无论是在开发还是测试或生产中都是用户未更改的静态数据.这种数据的常见例子包括国家代码用户角色.
>创建测试数据,夹具和工厂有两种一般方法.

> Fixtures是开箱即用的Rails方法.创建测试数据库时,创建它们一次.当您进行许多测试时,它们很快,因为它们只在测试套件的开头创建一次.然而,他们扭曲测试是因为它们鼓励在现有的固定装置上进行测试,所以我强烈建议您对它们进行测试.
>工厂是对象创建工具.您可以在每个测试中创建所需的对象,并将其删除或结束时回滚.使用工厂的大多数Rails项目都使用FactoryGirl.这就是我推荐的

这两种方法对象创建代码放在自己的文件中的不同于您的规范的目录中,从而避免了您的规范文件.如果您搜索SO“固定装置或工厂”,您将会发现更多的讨论.

如果您将所有重要的值(在这种情况下包括日期和金额)放在规范中,并将其与您所声明的结果进行比较,那么您的规格将更容易理解. (否则,您需要记住测试对象中的日期和金额以了解规范.)您可以在测试日期和金额参数中给出对象创建方法.那么你可能需要更少的方法.如果您使用FactoryGirl,可能只是一个指定每个对象的created_at和amount属性的问题.还要注意,Rails有一个方法,如1.day.from_now;如果您创建的对象以指定的方式指定,则可能不需要timecop.

关于如何在文件系统中布置RSpec规范,在顶层,只需使布局与Rails应用程序相同:

  1. app/
  2. controllers/
  3. bars_controller.rb
  4. foos_controller.rb
  5. models/
  6. bar.rb
  7. foo.rb
  8. ...
  9. ...
  10.  
  11. spec/
  12. controllers/
  13. bars_controller_spec.rb
  14. foos_controller_spec.rb
  15. ...
  16. models/
  17. bar_spec.rb
  18. foo_spec.rb
  19. ...
  20. ...

如果你的单一课程的规格太大,这是一个迹象表明课程太大了.找到一些模式来分解它们并单独测试.如果你真的不能分类(一个罕见的情况),将类的spec文件转换成spec文件的目录,如How to break down super long specs in RSpec?所述.

猜你在找的Ruby相关文章