Vedant Agarwala
Vedant Agarwala

Reputation: 18819

How to create a modular structure for rspec tests?

Currently I have code that looks like the following:

  it "Doesn't Access the Admin Panel"

  it "Doesn't see New Project button"

  it "Doesn't create new news"

  it "Doesn't access New Project page"

  it "Sees Projects"

  it "Sees news"

  it "Doesn't edit projects"

  it "Doesn't edit news"

As you can see almost all of the tests occur in pairs. Is there some way by which I can club these tests, like creating a method that accepts an argument whether it is a 'news' or 'project'?

OR this is not the way to do things? I want to follow best practices rather than try to create some new style of coding.

Upvotes: 0

Views: 39

Answers (1)

zetetic
zetetic

Reputation: 47548

You can nest examples in describe blocks using titles that reflect the context:

describe "When logged in as an admin" do
  let(:user) { admin_user }  
  it "can view the project" do
    # ...
  end
end

describe "When logged in as a guest" do
  let(:user) { guest_user }  
  it "cannot view the project" do
    # ...
  end
end

See the RSpec documentation for more examples.

Upvotes: 1

Related Questions