开发者

Repeated test descriptions with RSpec for every user role

Creating some controller tests with RSpec, I find myself repeating several test cases for every possible user role.

For example

describe "GET 'index'" do
  context "for admin user" do
    login_user("admin")

    it "has the right title" do
      response.should have_selector开发者_StackOverflow中文版("title", :content => "the title")
    end
  end

  context "for regular user" do
    login_user("user")

    it "has the right title" do
      response.should have_selector("title", :content => "the title")
    end
  end
end

This is a simple example just to make my point, but I have a lot of tests that are repeated... Of course there are also some tests that are unique for each context, but it doesn't matter here.

Is there a way to write the tests only once, and then run them in the different contexts?


Shared examples are a more flexible approach to this:

shared_examples_for "titled" do
  it "has the right title" do
    response.should have_selector("title", :content => "the title")
  end
end

And in the example

describe "GET 'index'" do
  context "for admin user" do
    login_user("admin")
    it_behaves_like "titled"
  end
end

Shared examples can also be included in other spec files to reduce duplication. This works well in controller tests when checking authentication/authorization, which often makes for repetitive tests.


describe "GET 'index'" do
  User::ROLES.each do |role|
    context "for #{role} user" do
      login_user(role)

      it "has the right title" do
        response.should have_selector("title", :content => "the title")
      end
    end
  end
end

You can use ruby's iterators in your specs. Given your particular implementation you'll have to adjust the code, but this give you the right idea for DRY'ing out your specs.

Also you'll need to make the necessary adjustments so your specs read well.


Try using shared example groups

http://relishapp.com/rspec/rspec-core/v/2-4/dir/example-groups/shared-example-group

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜