$ man jerome_

...

how i start my controller spec

| Comments

first, setup the data e.g.

1
2
let(:user) { FactoryGirl.create(:user) }
let(:admin) { FactoryGirl.create(:admin) }

then define who has access to it e.g.

1
2
3
4
5
6
7
8
describe "admin access" do
end

describe "user access" do
end

describe "guest access" do
end

if needed, i use shared examples e.g.

1
2
3
4
5
shared_examples("public access to products") do
end

shared_examples("full access to products") do
end

and call them like this:

1
it_behaves_like "public access to products"

here’s the full source for reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'spec_helper'

describe ProductsController do

  let(:user) { FactoryGirl.create(:user) }
  let(:admin) { FactoryGirl.create(:admin) }

  shared_examples("public access to products") do
    describe "GET #index" do
      before :each do
        get :index
      end

      it "renders the :index template"
    end
  end

  shared_examples("full access to products") do
    describe "GET 'new'" do
      before :each do
        get :new
      end

      it "renders the :new template"
    end
  end

  describe "admin access" do
    before :each do
      sign_in :user, admin
    end

    it_behaves_like "public access to products"
    it_behaves_like "full access to products"
  end

  describe "user access" do
    before :each do
      sign_in :user, user
    end

    it_behaves_like "public access to products"
    it_behaves_like "full access to products"
  end

  describe "guest access" do
    it_behaves_like "public access to products"

    describe "GET 'new'" do
      it "requires login"
    end
  end
end

Comments