-
Notifications
You must be signed in to change notification settings - Fork 8
add rspec messages #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,9 @@ gem 'jquery-rails' | |
# Use ActiveModel has_secure_password | ||
# gem 'bcrypt', '~> 3.1.7' | ||
|
||
# a library for generating fake data. | ||
gem 'faker' | ||
|
||
# Use Capistrano for deployment | ||
# gem 'capistrano-rails', group: :development | ||
|
||
|
@@ -44,18 +47,20 @@ group :development, :test do | |
|
||
# rspec | ||
gem 'rspec-rails', '~> 3.5' | ||
gem 'shoulda-matchers' | ||
|
||
# RSpec::JsonExpectations | ||
gem 'rspec-json_expectations' | ||
end | ||
|
||
group :test do | ||
# for build strategies | ||
gem 'factory_girl_rails' | ||
|
||
# database cleaner | ||
gem 'database_rewinder' | ||
# a library for setting up Ruby objects as test data. | ||
gem 'factory_girl_rails', '~> 4.0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you add version designation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Habit, I wouldn't like to be problem with oldest versions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I specified the lowest version in |
||
# shoulda-matcher provides Rspec and Minitest compatible one liners that test common Rails functionality. | ||
gem 'shoulda-matchers', '~> 3.1' | ||
# strategies for cleaning database in Ruby. | ||
gem 'database_cleaner' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to change from |
||
# an IRB alternative and runtime developer console. | ||
gem 'pry' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add it to development and test if necessary. |
||
end | ||
|
||
group :development do | ||
|
@@ -70,7 +75,7 @@ end | |
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem | ||
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] | ||
|
||
# json genarator | ||
gem 'active_model_serializers' | ||
# ActiveModel::Serializer implementation and Rails hooks. | ||
gem 'active_model_serializers', '~> 0.10.0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you add version designation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Habit, I wouldn't like to be problem with oldest versions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I specified the lowest version in |
||
|
||
gem 'foreman' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
module Api | ||
class ApiController < ActionController::API | ||
include Response | ||
include ExceptionHandler | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,41 +2,33 @@ module Api | |
class MessagesController < ApiController | ||
before_action :set_message, only: [:show, :update, :destroy] | ||
|
||
# GET /messages | ||
# GET / messages | ||
def index | ||
@messages = Message.order(created_at: :asc).all | ||
|
||
render json: @messages | ||
json_response(@messages) | ||
end | ||
|
||
# GET /messages/1 | ||
# GET / messages / :id | ||
def show | ||
render json: @message | ||
json_response(@message) | ||
end | ||
|
||
# POST /messages | ||
# POST / messages | ||
def create | ||
@message = Message.new(message_params) | ||
|
||
if @message.save | ||
render json: @message, status: :created, location: [:admin, @message] | ||
else | ||
render json: @message.errors, status: :unprocessable_entity | ||
end | ||
@message = Message.create!(message_params) | ||
json_response(@message, :created) | ||
end | ||
|
||
# PATCH/PUT /messages/1 | ||
# PUT / messages / :id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be patch. Please check |
||
def update | ||
if @message.update(message_params) | ||
render json: @message | ||
else | ||
render json: @message.errors, status: :unprocessable_entity | ||
end | ||
@message.update(message_params) | ||
head :no_content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for changing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the expected behavior in case of a |
||
end | ||
|
||
# DELETE /messages/1 | ||
# DELETE / messages / :id | ||
def destroy | ||
@message.destroy | ||
head :no_content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the expected behavior in case of a |
||
end | ||
|
||
private | ||
|
@@ -47,7 +39,7 @@ def set_message | |
|
||
# Only allow a trusted parameter "white list" through. | ||
def message_params | ||
params.require(:message).permit(:text) | ||
params.permit(:text) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for changing? |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ExceptionHandler | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
rescue_from ActiveRecord::RecordNotFound do |e| | ||
json_response({ message: e.message }, :not_found) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since modules are not loosely coupled, it is not recommended to do such a construction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since browsers only read 4xx & 5xx error codes, all Rails exceptions have to be inferred. Thus, ExceptionHandler simply has to manage how the 4xx / 5xx errors are passed to the browser and it provides the more graceful |
||
end | ||
|
||
rescue_from ActiveRecord::RecordInvalid do |e| | ||
json_response({ message: e.message }, :unprocessable_entity) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Response | ||
def json_response(object, status = :ok) | ||
render json: object, status: status | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
class Message < ApplicationRecord | ||
# model association | ||
has_many :reactions, dependent: :destroy | ||
# validation | ||
validates_presence_of :text | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryGirl.define do | ||
factory :message do | ||
text { Faker::Lorem.sentences } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Message, type: :model do | ||
#pending "add some examples to (or delete) #{__FILE__}" | ||
# association test | ||
# ensure Message model has a 1:m relationship with the Reaction model | ||
it { should have_many(:reactions).dependent(:destroy) } | ||
# validation tests | ||
# ensure columns text is present before saving | ||
it { should validate_presence_of(:text) } | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,105 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Messages", type: :request do | ||
describe "GET /messages" do | ||
it "works! (now write some real specs)" do | ||
get api_messages_path | ||
RSpec.describe "Messages API", type: :request do | ||
# initialize tests data | ||
let!(:messages) { create_list(:message, 10) } | ||
let(:message_id) { messages.first.id } | ||
|
||
# test suite for GET / messages | ||
describe "GET / messages" do | ||
before { get "/api/messages" } | ||
|
||
it "returns status code 200" do | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it "returns messages" do | ||
expect(json).not_to be_empty | ||
expect(json.size).to eq(10) | ||
end | ||
end | ||
|
||
# test suite for GET / messages / :id | ||
describe "GET / messages / :id" do | ||
before { get "/api/messages/#{message_id}" } | ||
|
||
context "when the record exists" do | ||
it "returns status code 200" do | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it "returns the message" do | ||
expect(json).not_to be_empty | ||
expect(json['id']).to eq(message_id) | ||
end | ||
end | ||
|
||
context "when the record does not exist" do | ||
let(:message_id) { 100 } | ||
|
||
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 Message/) | ||
end | ||
end | ||
end | ||
|
||
# test suite for POST / messages | ||
describe "POST / messages" do | ||
let(:valid_attributes) { { text: "Text" } } | ||
let(:invalid_attributes) { {} } | ||
|
||
context "when the request is valid" do | ||
before { post "/api/messages", params: valid_attributes } | ||
|
||
it "returns status code 201" do | ||
expect(response).to have_http_status(201) | ||
end | ||
|
||
it "creates a message" do | ||
expect(json['text']).to eq("Text") | ||
end | ||
end | ||
|
||
describe "when the request is invalid" do | ||
before { post "/api/messages", params: invalid_attributes } | ||
|
||
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(/Validation failed: Text can't be blank/) | ||
end | ||
end | ||
end | ||
|
||
# test suite for PUT / messages / :id | ||
describe "PUT / messages / :id" do | ||
let(:valid_attributes) { { text: "Text" } } | ||
|
||
context "when the record exists" do | ||
before { put "/api/messages/#{message_id}", params: valid_attributes } | ||
|
||
it "returns status code 204" do | ||
expect(response).to have_http_status(204) | ||
end | ||
|
||
it "updates the record" do | ||
expect(response.body).to be_empty | ||
end | ||
end | ||
end | ||
|
||
# test suite for DELETE / messages / :id | ||
describe "DELETE / messages / :id" do | ||
before { delete "/api/messages/#{message_id}" } | ||
|
||
it "returns status code 204" do | ||
expect(response).to have_http_status(204) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is desirable to be in the test group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like use
gem faker
to generate data at random indb/seeds.rb
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so, should you limit it with
development
andtest
? We do not need it forstaging
orproduction
.