OSDN Git Service

Regular updates
[twpd/master.git] / rspec-rails.md
1 ---
2 title: Rspec-rails
3 category: Ruby
4 ---
5
6 ### Spec tasks
7
8     rake spec:controllers
9     rake spec:helpers
10     rake spec:lib
11     rake spec:mailers
12     rake spec:models
13     rake spec:requests
14     rake spec:routing
15     rake spec:views
16
17 ### Models
18
19 ```rb
20 # spec/models/*.rb
21 describe MyModel do
22 end
23 ```
24
25 ### Controllers
26
27 ```rb
28 # spec/controllers/*.rb
29 describe MyController do
30   describe "POST update" do
31     render_views #optional
32
33     it "works" do
34       post :update, { user: { name: "john" } }
35
36       controller
37       controller.send ...
38
39       response
40       expect(response).to be_success
41       expect(response).to have_http_status(200)
42       expect(response).to render_template("index")
43       expect(response).to redirect_to '/..'
44
45       expect(assigns :article).to eq article
46
47       response.status
48     end
49   end
50 end
51 ```
52
53 ### Request
54
55 ```js
56 # spec/requests/*.rb
57 describe "home page" do
58   it "displays the user's username after successful login" do
59     get "/login"
60     post "/login", username: "jdoe", password: "secret"
61
62     expect(response.status).to eql 200
63     expect(response).to redirect_to(...)
64     expect(response).to render_template(:show)
65     expect(response.body).to include 'hello'
66     follow_redirect!
67   end
68 end
69 ```
70
71 ### Routing
72
73 ```rb
74 # spec/routing/*.rb
75 describe "routing to profiles" do
76   it "routes /profile/:username to profile#show for username" do
77     expect(get: "/profiles/jsmith").to route_to(
78       controller: "profiles",
79       action: "show",
80       username: "jsmith"
81     )
82   end
83
84   it "does not expose a list of profiles" do
85     expect(get: "/profiles").not_to be_routable
86   end
87 end
88 ```
89
90 ### Helpers
91
92 ```rb
93 # spec/helpers/*.rb
94 describe EventsHelper do
95   describe "#link_to_event" do
96     it "displays the title, and formatted date" do
97       event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
98
99       # helper is an instance of ActionView::Base configured with the
100       # EventsHelper and all of Rails' built-in helpers
101       expect(helper.link_to_event).to match /Ruby Kaigi, 27 Aug, 2010/
102     end
103   end
104 end
105 ```
106
107 ### Features
108
109 ```rb
110 # spec/features/*.rb
111 feature 'Signing in' do
112   given(:something) { "hi" }
113
114   background do
115     User.make email: 'hi@gmail.com'
116   end
117
118   scenario 'Signing in with credentials' do
119   end
120 end
121 ```
122
123 ### Matchers
124
125 ```rb
126 be_a_new(Widget)  # new_record?
127 render_template("new")
128 render_template(partial: 'form', locals: {...})
129 redirect_to(widgets_path)
130 route_to(..)
131 be_routable
132 have_http_status(500)
133 have_http_status(:created)
134 ```
135
136 ### Time helpers
137
138 ```
139 travel_to Time.new(2014, 11, 14, 01, 04, 44)
140 ...
141 travel_back
142
143 travel_to Time.new(2014, 11, 14, 01, 04, 44) do
144   ...
145 end
146 ```