jpw
jpw

Reputation: 19257

rspec and rails and capybara: how to use same test to test 6 different variations of a factory

Our app supports multiple type of accounts so a lot of our tests iterate across all the account types.

However, a common pattern we have is the first steps are the same for each account type, but MID-way thru we want to test some of the unique aspects of each account type. Here's a high level view of one test:

all_account_types.each do |account_type|

That last test is the issue... the test we run depends on the type of account... Currently we have logic inside the test that says if account_type==X then Y.should include(Z) and that works as far as doing the test, but it would be nicer

Upvotes: 2

Views: 202

Answers (1)

zetetic
zetetic

Reputation: 47578

Don't forget that let is lazy-evaluated, which means you can override the test setup before an example or context:

context "Accounts" do
  let(:account) { Factory.create(:generic_account) }

  it "behaves like an account" do
    # ...
  end

  context "Manager account" do
    let(:account) { Factory.create(:manager_account }

    it "behaves like a manager account" do
       account.type.should == "manager"
       # ...
    end
  end
end

Upvotes: 1

Related Questions