如何使用RSpec + Capybara在不同功能内重用场景 [英] How to reuse scenarios within different features with rspec + capybara

查看:85
本文介绍了如何使用RSpec + Capybara在不同功能内重用场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一些要在不同上下文或功能"下进行测试的方案.

Say I have some scenarios that I want to test under different contexts, or "features".

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

For example, I have scenarios which involve the user visiting certain pages and expecting certain ajax results.

但是,在不同的条件或功能"下,我需要执行不同的后台"任务来更改应用程序的状态.

But, under different conditions, or "features", I need to perform different "background" tasks which change the state of the app.

在这种情况下,我需要一遍又一遍地运行相同的方案,以确保一切都能在应用程序状态进行不同的更改后得以实现.

In this case I need to run the same scenarios over and over again to make sure that everything works with the different changes to the state of the app.

有没有一种方法可以在某处定义方案,然后再使用它们?

Is there a way to define scenarios somewhere, and then reuse them?

推荐答案

您可以使用下面是从relishapp页面获取的一个基本示例.如您所见,在多个功能中使用相同的场景来测试不同的类-即运行了6个示例.

A basic example, taken from the relishapp page is below. As you can see, the same scenarios are used in multiple features to test different classes - ie there are 6 examples run.

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页面上有几个示例,包括使用参数运行共享的示例(在下面复制).我猜(因为我不知道您的确切测试),您应该能够在执行示例集之前使用参数来设置不同的条件.

There are several examples on the relishapp page, including running the shared examples with parameters (copied below). I would guess (since I do not know your exact tests) that you should be able to use the parameters to setup the different conditions prior to executing the set of examples.

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, 2, 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, [:size, :length]
end

这篇关于如何使用RSpec + Capybara在不同功能内重用场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆