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

ruby-on-rails-3 – 如何使用rspec capybara重用不同功能中的场

发布时间:2020-12-16 22:53:12 所属栏目:百科 来源:网络整理
导读:假设我有一些我想在不同的上下文或“功能”下测试的场景. 例如,我有一些场景涉及用户访问某些页面并期望某些ajax结果. 但是,在不同的条件或“功能”下,我需要执行不同的“后台”任务,这些任务会改变应用程序的状态. 在这种情况下,我需要一遍又一遍地运行相同
假设我有一些我想在不同的上下文或“功能”下测试的场景.

例如,我有一些场景涉及用户访问某些页面并期望某些ajax结果.

但是,在不同的条件或“功能”下,我需要执行不同的“后台”任务,这些任务会改变应用程序的状态.

在这种情况下,我需要一遍又一遍地运行相同的场景,以确保一切都适用于应用程序状态的不同更改.

有没有办法在某处定义场景,然后重用它们?

解决方法

您可以使用 shared examples创建在多个功能中使用的可重用方案.

从relishapp页面获取的基本示例如下.如您所见,在多个功能中使用相同的方案来测试不同的类 – 即运行了6个示例.

require 'rspec/autorun'
require "set"

shared_examples "a collection" do
  let(:collection) { described_class.new([7,2,4]) }

  context "initialized with 3 items" do
    it "says it has three items" do
      collection.size.should eq(3)
    end
  end

  describe "#include?" do
    context "with an an item that is in the collection" do
      it "returns true" do
        collection.include?(7).should be_true
      end
    end

    context "with an an item that is not in the collection" do
      it "returns false" do
        collection.include?(9).should be_false
      end
    end
  end
end

describe Array do
  it_behaves_like "a collection"
end

describe Set do
  it_behaves_like "a collection"
end

relishapp页面上有几个示例,包括使用参数运行共享示例(下面复制).我猜(因为我不知道你的确切测试)你应该能够在执行一组例子之前使用参数来设置不同的条件.

require 'rspec/autorun'

shared_examples "a measurable object" do |measurement,measurement_methods|
  measurement_methods.each do |measurement_method|
    it "should return #{measurement} from ##{measurement_method}" do
      subject.send(measurement_method).should == measurement
    end
  end
end

describe Array,"with 3 items" do
  subject { [1,3] }
  it_should_behave_like "a measurable object",3,[:size,:length]
end

describe String,"of 6 characters" do
  subject { "FooBar" }
  it_should_behave_like "a measurable object",6,:length]
end

(编辑:李大同)

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

    推荐文章
      热点阅读