开发者

With Rspec, run test for each of several options?

I want to run a given suite of tests several times - once as each of the basic types 开发者_C百科of system users. (It tests the various features that should be shared by them). More generally, I often end up wanting to run the same set of tests under different conditions.

run_as_each(user_list) do |user|
  describe "When visiting the front page with #{user.name}" do
    it "should see the welcome message" do
      ....
    end
    it "should be able to login" do
      ....
    end
  end     
end

But this isn't working - It says my method "run_as_each" is undefined - in fact, it seems helpers can only be used inside of the actual specific tests, "it"s. So what should I do?

So what is another way I can approach this problem - specifically, to run a batch of tests for several different users, or at the least to define within a test which users it should be run for?


Have you looked into using shared example? You mentioned types of users, so something like this might work.

shared_examples "a user" do
  let(:user) { described_class.new }

  describe "When visiting the front page" do
    it "should see the welcome message" do
      ....
    end
    it "should be able to login" do
      ....
    end
  end
end

describe UserTypeA do
  it_behaves_like 'a user'
end

describe UserTypeB do
  it_behaves_like 'a user'
end

I didn't run the code, so syntax errors are probable. More from the docs


I was able to get around this problem by including a file with a namespaced module that included all the "helper" methods I needed to use for dynamic code generation. The actual test helpers, if included through the rspec config, are only loaded when the tests are being executed - they are, thus, not available to the any code that occurs outside of tests.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜