summaryrefslogtreecommitdiff
path: root/spec/controllers/tutorials_controller_spec.rb
blob: 3bf1a779c2ab0174ca76cff2413a99e680c7b6e7 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'spec_helper'

describe TutorialsController do
  def valid_attributes
    {:url => 'http://blah.com', :heading => "hello world"}
  end

  let(:user){ create(:user) }

  before (:each) do
    request.env['warden'] = double(Warden, :authenticate => user, :authenticate! => user)
  end

  describe :index do
    let(:tutorial) { create(:tutorial) }

    before :each do
      user.tutorials << tutorial
      get :index
    end

    it "assigns all tutorials as @tutorials" do
      assigns(:tutorials).should include(tutorial)
    end
  end

  describe :show do
    let(:tutorial) { create(:tutorial) }

    before :each do
      user.tutorials << tutorial
      get :show, :id => tutorial.to_param
    end

    it "assigns the requested tutorial" do
      assigns(:tutorial).should == tutorial
    end
  end

  describe :new do
    it "assigns a new tutorial as @tutorial" do
      get :new
      assigns(:tutorial).should be_a_new(Tutorial)
    end
  end

  describe :edit do
    let(:tutorial) { create(:tutorial) }

    it "assigns the requested tutorial as @tutorial" do
      user.tutorials << tutorial
      get :edit, {:id => tutorial.to_param}
      assigns(:tutorial).should eq(tutorial)
    end
  end

  describe :create do
    describe "with valid params" do
      before :each do
        post :create, {:tutorial => {:url => 'http://blah.com', :heading => "hello world"} }
      end

      it "creates a new Tutorial" do
        Tutorial.count.should == 1
      end

      it "assigns a newly created tutorial as @tutorial" do
        assigns(:tutorial).should be_a(Tutorial)
        assigns(:tutorial).should be_persisted
        assigns(:tutorial).url.should == 'http://blah.com'
        assigns(:tutorial).heading.should == 'hello world'
      end

      it "redirects to the created tutorial" do
        response.should redirect_to(my_dashboard_path)
      end
    end

    describe "with invalid params" do
      before :each do
        Tutorial.any_instance.stub(:save).and_return(false)
        post :create, {:tutorial => {:url => '', :heading => ''}}
      end

      it "assigns a newly created but unsaved tutorial as @tutorial" do
        assigns(:tutorial).should be_a_new(Tutorial)
      end

      it "re-renders the 'new' template" do
        response.should render_template("new")
      end

      it "should display an error" do
        flash[:error].should_not be_nil
      end
    end
  end

  describe :patch do
    describe "with valid params" do
      let(:tutorial) { create(:tutorial) }

      before :each do
        user.tutorials << tutorial
        patch :update, :id => tutorial.to_param, :tutorial => { :url => 'http://blah', :heading => 'headless'}
      end

      it "assigns the requested tutorial" do
        assigns(:tutorial).should == tutorial
        assigns(:tutorial).url.should == 'http://blah'
        assigns(:tutorial).heading.should == 'headless'
      end

      it "redirects to the tutorial" do
        response.should redirect_to(tutorial.reload)
      end
    end

    describe "with invalid params" do
      let(:tutorial) { create(:tutorial) }
      before :each do
        user.tutorials << tutorial
        Tutorial.any_instance.stub(:save).and_return(false)
        patch :update, {:id => tutorial.to_param, :tutorial => {:url => "", :heading => ""}}
      end

      it "assigns the tutorial as @tutorial" do
        assigns(:tutorial).should eq(tutorial)
      end

      it "re-renders the 'edit' template" do
        response.should render_template("edit")
      end
    end
  end

  describe :destroy do
    let(:tutorial) { create(:tutorial) }

    before :each do
      user.tutorials << tutorial
      delete :destroy, {:id => tutorial.to_param}
    end

    it "destroys the requested tutorial" do
      Tutorial.count.should == 0
    end

    it "redirects to the tutorials list" do
      response.should redirect_to(my_dashboard_path)
    end
  end
end