大佬教程收集整理的这篇文章主要介绍了ruby – 使用和不使用Rails进行快速(Rspec)测试,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1.SALE是ActiveRecord的子类;它的工作是将销售数据保存到数据库中.
class SALE < ActiveRecord::Base def self.@R_450_10586@l_for_duration(start_date,END_DATE) self.count(conditions: {date: start_date..END_DATE}) end #... end
2.SALEsReport是一个标准的Ruby类;它的工作是生成和绘制有关销售的信息.
class SALEsReport def initialize(start_date,END_DATE) @start_date = start_date @END_DATE = END_DATE end def SALEs_in_duration SALE.@R_450_10586@l_for_duration(@start_date,@END_DATE) end #... end
因为我想使用TDD并且我希望我的测试运行得非常快,所以我为SALEsReport编写了一个不加载Rails的规范:
require_relative "../../app/models/SALEs_report.rb" class SALE; end # NOTE I have had to re-define SALE because I don't want to # require `SALE.rb` because it would then require ActiveRecord. describe SALEsReport do describe "SALEs_in_duration" do it "calls SALE.@R_450_10586@l_for_duration" do SALE.should_receive(:@R_450_10586@l_for_duration) SALEsReport.new.SALEs_in_duration end end end
当我运行bundle exec rspec spec / models / report_spec.rb时,此测试有效.
但是,当我运行bundle exec rake spec并且类SALE(TypeError)的错误超类不匹配时,此测试失败.我知道错误正在发生,因为Tap是由SALE.rb定义并在规范中内联.
所以我的问题是,如果没有定义该类,有一种方法可以将stub(或mock或DoublE)作为一个类吗?这将允许我删除内联类SALE;结束,这感觉就像一个黑客.
如果没有,如何运行bundle exec rspec或bundle exec rake spec,我如何设置我的测试以使它们正确运行?
如果没有,我写快速测试的方法是错误的吗?!
最后,我不想使用Spork.谢谢!
describe SALEsReport do before { stub_const("SALE",Class.new) } describe "SALEs_in_duration" do it "calls SALE.@R_450_10586@l_for_duration" do SALE.should_receive(:@R_450_10586@l_for_duration) SALEsReport.new.SALEs_in_duration end end end
您可能还想使用rspec-fire来代替SALE来使用测试双精度来自动检查真实SALE类中存在的所有模拟/存根方法在运行带有加载的真实SALE类的测试时(例如,当您运行测试套件时) :
require 'rspec/fire' describe SALEsReport do include RSpec::Fire describe "SALEs_in_duration" do it "calls SALE.@R_450_10586@l_for_duration" do fire_replaced_class_double("SALE") SALE.should_receive(:@R_450_10586@l_for_duration) SALEsReport.new.SALEs_in_duration end end end
如果在真正的SALE类上重命名@R_450_10586@l_for_duration,则在模拟方法时rspec-fire会给出错误,因为它在实际类中不存在.
以上是大佬教程为你收集整理的ruby – 使用和不使用Rails进行快速(Rspec)测试全部内容,希望文章能够帮你解决ruby – 使用和不使用Rails进行快速(Rspec)测试所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。