加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 当使用rspec进行测试时,在哪里放置常用的“测

发布时间:2020-12-16 20:33:36 所属栏目:百科 来源:网络整理
导读:假设你有一个销售小部件的购物网站.但是,每个小部件的清单都是有限的,所以保持“widget.number_still_available”号码是最新的. 我想编写一个rspec测试 it "always displays the correct number still available" do # Assume there is a before method that
假设你有一个销售小部件的购物网站.但是,每个小部件的清单都是有限的,所以保持“widget.number_still_available”号码是最新的.

我想编写一个rspec测试

it "always displays the correct number still available" do

   # Assume there is a before method that sets up a widget with 5 available

   widget.number_still_available.should == 5

   # User "a@b.com" purchases 2 widgets
   widget.number_still_available.should == 3

   # User "c@d.com" purchases 1 widget
   widget.number_still_available.shhould == 2

   # User "a@b.com" cancels purchase of 1 widget
   widget.number_still_available.should == 4
end

我想要能够编写仅测试的方法来执行“购买”和“取消”方法.由于各种原因,这些操作与我的模型中的任何“真实”方法都不相符(最重要的是在PHP中有一个解耦的后端系统执行购买和取消操作的一部分).

在使用RSpec时,放置此代码的正确位置在哪里?在黄瓜中,我可以写几步 – 但我不知道RSpec是什么正确的等价物.

解决方法

我建议在spec / support中添加一个名为purchase_helpers.rb的新文件,并将其中的内容放入其中:
module PurchaseHelpers
  def purchase_widgets(user,count=1)
    # Code goes here
  end

  def cancel_purchase(user,count=1)
    # Code goes here
  end
end

RSpec.configure do |c|
  c.include PurchaseHelpers
end

这样做的好处不是将其嵌入spec / spec_helper.rb中,它并没有将这个文件与拥有大量不相关的RSPec代码组合在一起.把事情分开是做事情的最好办法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读