cheatsheets/rspec-rails.md

155 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2015-03-29 17:19:15 +00:00
---
2015-11-22 23:43:09 +00:00
title: Rspec-rails
2015-11-24 04:30:17 +00:00
category: Ruby
2015-03-29 17:19:15 +00:00
---
2023-03-13 12:02:33 +00:00
### About
{: .-intro}
RSpec is a Ruby library for testing. [rspec-rails](https://github.com/rspec/rspec-rails) is its Rails integration.
- <https://rspec.info/>
- <https://github.com/rspec/rspec-rails>
2015-05-22 15:36:13 +00:00
### Spec tasks
rake spec:controllers
rake spec:helpers
rake spec:lib
rake spec:mailers
rake spec:models
rake spec:requests
rake spec:routing
rake spec:views
2015-03-29 17:19:15 +00:00
### Models
```rb
# spec/models/*.rb
2017-10-22 11:08:21 +00:00
describe MyModel do
2015-03-29 17:19:15 +00:00
end
```
### Controllers
```rb
# spec/controllers/*.rb
describe MyController do
describe "POST update" do
2015-05-05 16:00:33 +00:00
render_views #optional
2015-03-29 17:19:15 +00:00
it "works" do
post :update, { user: { name: "john" } }
controller
controller.send ...
response
expect(response).to be_success
expect(response).to have_http_status(200)
expect(response).to render_template("index")
2015-11-17 10:25:36 +00:00
expect(response).to redirect_to '/..'
expect(assigns :article).to eq article
2015-05-05 16:00:33 +00:00
response.status
2015-03-29 17:19:15 +00:00
end
end
end
```
### Request
```js
# spec/requests/*.rb
describe "home page" do
it "displays the user's username after successful login" do
get "/login"
post "/login", username: "jdoe", password: "secret"
2015-04-20 11:18:58 +00:00
expect(response.status).to eql 200
2016-04-25 07:55:29 +00:00
expect(response).to redirect_to(...)
expect(response).to render_template(:show)
expect(response.body).to include 'hello'
follow_redirect!
2015-03-29 17:19:15 +00:00
end
end
```
### Routing
```rb
# spec/routing/*.rb
describe "routing to profiles" do
it "routes /profile/:username to profile#show for username" do
expect(get: "/profiles/jsmith").to route_to(
controller: "profiles",
action: "show",
username: "jsmith"
)
end
it "does not expose a list of profiles" do
expect(get: "/profiles").not_to be_routable
end
end
```
### Helpers
```rb
# spec/helpers/*.rb
describe EventsHelper do
describe "#link_to_event" do
it "displays the title, and formatted date" do
event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
# helper is an instance of ActionView::Base configured with the
# EventsHelper and all of Rails' built-in helpers
expect(helper.link_to_event).to match /Ruby Kaigi, 27 Aug, 2010/
end
end
end
```
2015-11-17 10:25:36 +00:00
### Features
```rb
# spec/features/*.rb
feature 'Signing in' do
2015-11-23 10:56:05 +00:00
given(:something) { "hi" }
2015-11-17 10:25:36 +00:00
background do
User.make email: 'hi@gmail.com'
end
scenario 'Signing in with credentials' do
end
end
```
2015-03-29 17:19:15 +00:00
### Matchers
```rb
be_a_new(Widget) # new_record?
render_template("new")
render_template(partial: 'form', locals: {...})
redirect_to(widgets_path)
route_to(..)
be_routable
have_http_status(500)
have_http_status(:created)
```
2015-11-24 04:30:17 +00:00
### Time helpers
```
travel_to Time.new(2014, 11, 14, 01, 04, 44)
...
travel_back
travel_to Time.new(2014, 11, 14, 01, 04, 44) do
...
end
```