Created
September 21, 2017 13:22
-
-
Save yokolet/64381a2b9943f2f28ba1878f3e41f18c to your computer and use it in GitHub Desktop.
rspec request specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe "Posts", type: :request do | |
let!(:posts) { create_list(:post, 10) } | |
let(:post_id) { posts.last.id } | |
describe "GET /posts" do | |
before { get posts_path } | |
it "returns status code 200" do | |
expect(response).to have_http_status(200) | |
end | |
it "returns posts" do | |
json = JSON.parse(response.body) | |
expect(json).not_to be_empty | |
expect(json.size).to eq(10) | |
end | |
end | |
describe "GET /posts/:id" do | |
before { get post_path(post_id) } | |
context "when the post exists" do | |
it "returns status code 200" do | |
expect(response).to have_http_status(200) | |
end | |
it "returns the post" do | |
json = JSON.parse(response.body) | |
expect(json).not_to be_empty | |
expect(json['id']).to eq(post_id) | |
end | |
end | |
context "when the post id is wrong" do | |
let(:post_id) { 1000 } | |
it "returns status code 404" do | |
expect(response).to have_http_status(404) | |
end | |
it "returns a not found message" do | |
expect(response.body).to match(/Couldn't find Post/) | |
end | |
end | |
end | |
describe "POST /posts" do | |
let(:attrs) { {post: {subject: "My Post Test", | |
content: "How does this work?"}} } | |
context "when the post data is valid" do | |
before { post posts_path, params: attrs } | |
it "returns status code 201" do | |
expect(response).to have_http_status(201) | |
end | |
it "creates a post" do | |
json = JSON.parse(response.body) | |
expect(json["subject"]).to eq("My Post Test") | |
expect(json["content"]).to eq("How does this work?") | |
end | |
end | |
context "when the post data is invalid" do | |
before { post posts_path, params: {post: {subject: "wrong"}} } | |
it "returns status code 422" do | |
expect(response).to have_http_status(422) | |
end | |
it "returns a validation failure message" do | |
expect(response.body).to match(/can't be blank/) | |
end | |
end | |
end | |
describe "PUT /posts/:id" do | |
let(:attr) { { post: { content: "It works exactly like this." }}} | |
context "when the post exists" do | |
before { put post_path(post_id), params: attr } | |
it "returns status code 200" do | |
expect(response).to have_http_status(200) | |
end | |
it "updates the post" do | |
json = JSON.parse(response.body) | |
expect(json["content"]).to eq("It works exactly like this.") | |
end | |
end | |
end | |
describe "DELETE /posts/:id" do | |
before { delete post_path(post_id) } | |
it "returns status code 204" do | |
expect(response).to have_http_status(204) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment