josevalim (owner)

Forks

Revisions

gist: 94700 Download_button fork
public
Description:
Comparision of Remarkable way with Rspec way of testing controllers
Public Clone URL: git://gist.github.com/94700.git
Embed All Files: show embed
remarkable_way.rb #
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 
describe TasksController do
  mock_models :task
  
  describe :get => :index do
    expects :find, :on => Task, :with => :all, :returns => mock_task
    should_assign_to :tasks, :with => mock_task
 
    describe Mime::XML do
      expects :to_xml, :on => mock_task, :returns => 'generated xml'
 
      should_assign_to :tasks, :with => mock_task
      should_respond_with :body => /generated_xml/, :content_type => Mime::XML
    end
  end
 
  describe :get => :show, :id => '37' do
    expects :find, :on => Task, :with => '37', :returns => mock_task
    should_assign_to :task, :with => mock_task
    
    describe Mime::XML do
      expects :to_xml, :on => mock_task, :returns => 'generated xml'
 
      should_assign_to :task, :with => mock_task
      should_respond_with :body => /generated_xml/, :content_type => Mime::XML
    end
  end
 
  describe :get => :new do
    expects :new, :on => Task, :returns => mock_task
    should_assign_to :task, :with => mock_task
  end
 
  describe :get => :edit, :id => '37' do
    expects :find, :on => Task, :with => '37', :returns => mock_task
    should_assign_to :task, :with => mock_task
  end
 
  describe :post => :create, :task => {:these => 'params'} do
    expects :new, :on => Task, :with => {'these' => 'params'}, :returns => mock_task
 
    describe "with valid params" do
      expects :save, :on => mock_task, :returns => true
 
      should_assign_to :task, :with => mock_task
      should_redirect_to { task_url(mock_task) }
    end
   
    describe "with invalid params" do
      expects :save, :on => mock_task, :returns => false
 
      should_assign_to :task, :with => mock_task
      should_render_template 'new'
    end
  end
 
  describe :put => :update, :id => "37", :task => {:these => 'params'} do
    expects :find, :on => Task, :with => '37', :returns => mock_task
 
    describe "with valid params" do
      expects :update_attributes, :on => mock_task, :with => {'these' => 'params'}, :returns => true
 
      should_assign_to :task, :with => mock_task
      should_redirect_to { task_url(mock_task) }
    end
    
    describe "with invalid params" do
      expects :update_attributes, :on => mock_task, :with => {'these' => 'params'}, :returns => false
 
      should_assign_to :task, :with => mock_task
      should_render_template 'edit'
    end
  end
 
  describe :delete => :destroy do
    expects :find, :on => Task, :with => '37', :returns => mock_task
    expects :destroy, :on => mock_task
 
    should_assign_to :task, :with => mock_task
    should_redirect_to { tasks_url }
  end
end
rspec_way.rb #
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 
describe TasksController do
 
  def mock_task(stubs={})
    @mock_task ||= mock_model(Task, stubs)
  end
  
  describe "GET index" do
 
    it "exposes all tasks as @tasks" do
      Task.should_receive(:find).with(:all).and_return([mock_task])
      get :index
      assigns[:tasks].should == [mock_task]
    end
 
    describe "with mime type of xml" do
  
      it "renders all tasks as xml" do
        Task.should_receive(:find).with(:all).and_return(tasks = mock("Array of Tasks"))
        tasks.should_receive(:to_xml).and_return("generated XML")
        get :index, :format => 'xml'
        response.body.should == "generated XML"
      end
    
    end
 
  end
 
  describe "GET show" do
 
    it "exposes the requested task as @task" do
      Task.should_receive(:find).with("37").and_return(mock_task)
      get :show, :id => "37"
      assigns[:task].should equal(mock_task)
    end
    
    describe "with mime type of xml" do
 
      it "renders the requested task as xml" do
        Task.should_receive(:find).with("37").and_return(mock_task)
        mock_task.should_receive(:to_xml).and_return("generated XML")
        get :show, :id => "37", :format => 'xml'
        response.body.should == "generated XML"
      end
 
    end
    
  end
 
  describe "GET new" do
  
    it "exposes a new task as @task" do
      Task.should_receive(:new).and_return(mock_task)
      get :new
      assigns[:task].should equal(mock_task)
    end
 
  end
 
  describe "GET edit" do
  
    it "exposes the requested task as @task" do
      Task.should_receive(:find).with("37").and_return(mock_task)
      get :edit, :id => "37"
      assigns[:task].should equal(mock_task)
    end
 
  end
 
  describe "POST create" do
 
    describe "with valid params" do
      
      it "exposes a newly created task as @task" do
        Task.should_receive(:new).with({'these' => 'params'}).and_return(mock_task(:save => true))
        post :create, :task => {:these => 'params'}
        assigns(:task).should equal(mock_task)
      end
 
      it "redirects to the created task" do
        Task.stub!(:new).and_return(mock_task(:save => true))
        post :create, :task => {}
        response.should redirect_to(task_url(mock_task))
      end
      
    end
    
    describe "with invalid params" do
 
      it "exposes a newly created but unsaved task as @task" do
        Task.stub!(:new).with({'these' => 'params'}).and_return(mock_task(:save => false))
        post :create, :task => {:these => 'params'}
        assigns(:task).should equal(mock_task)
      end
 
      it "re-renders the 'new' template" do
        Task.stub!(:new).and_return(mock_task(:save => false))
        post :create, :task => {}
        response.should render_template('new')
      end
      
    end
    
  end
 
  describe "PUT udpate" do
 
    describe "with valid params" do
 
      it "updates the requested task" do
        Task.should_receive(:find).with("37").and_return(mock_task)
        mock_task.should_receive(:update_attributes).with({'these' => 'params'})
        put :update, :id => "37", :task => {:these => 'params'}
      end
 
      it "exposes the requested task as @task" do
        Task.stub!(:find).and_return(mock_task(:update_attributes => true))
        put :update, :id => "1"
        assigns(:task).should equal(mock_task)
      end
 
      it "redirects to the task" do
        Task.stub!(:find).and_return(mock_task(:update_attributes => true))
        put :update, :id => "1"
        response.should redirect_to(task_url(mock_task))
      end
 
    end
    
    describe "with invalid params" do
 
      it "updates the requested task" do
        Task.should_receive(:find).with("37").and_return(mock_task)
        mock_task.should_receive(:update_attributes).with({'these' => 'params'})
        put :update, :id => "37", :task => {:these => 'params'}
      end
 
      it "exposes the task as @task" do
        Task.stub!(:find).and_return(mock_task(:update_attributes => false))
        put :update, :id => "1"
        assigns(:task).should equal(mock_task)
      end
 
      it "re-renders the 'edit' template" do
        Task.stub!(:find).and_return(mock_task(:update_attributes => false))
        put :update, :id => "1"
        response.should render_template('edit')
      end
 
    end
 
  end
 
  describe "DELETE destroy" do
 
    it "destroys the requested task" do
      Task.should_receive(:find).with("37").and_return(mock_task)
      mock_task.should_receive(:destroy)
      delete :destroy, :id => "37"
    end
  
    it "redirects to the tasks list" do
      Task.stub!(:find).and_return(mock_task(:destroy => true))
      delete :destroy, :id => "1"
      response.should redirect_to(tasks_url)
    end
 
  end
 
end