Answer the question
In order to leave comments, you need to log in
How to test a module?
There is an authenticable.rb
module :
module Authenticable
def current_user
@current_user ||= User.find_by(auth_token: request.headers['Authorization'])
end
def authenticate_with_token!
render json: { errors: "Not authenticated" },
status: :unauthorized unless user_signed_in?
end
def user_signed_in?
current_user.present?
end
end
# app/controllers/api/v1/appication_controller.rb
class Api::V1::ApplicationController < ActionController::Base
protect_from_forgery with: :null_session
include Authenticable
end
# app/controllers/api/v1/users_controller.rb
class Api::V1::UsersController < Api::V1::ApplicationController
respond_to :json
before_filter :authenticate_with_token!, only: [:update, :destroy]
....
.....
end
require 'rails_helper'
class Authentication < ActionController::Base
include Authenticable
end
describe Authenticable do
let(:authentication) { Authentication.new }
subject { authentication }
describe "#authenticate_with_token" do
before do
authentication.stub(:current_user).and_return(nil)
??????
end
it "render a json error message" do
expect(json_response[:errors]).to eql "Not authenticated"
end
it { response.status.should eq 401 }
end
Answer the question
In order to leave comments, you need to log in
describe '.authenticate_with_token' do
before do
allow(authentication).to receive(:current_user).and_return(nil)
allow(authentication).to receive(:render) do |args| # возвращаем аргументы
args
end
end
it 'returns error' do
expect(authentication.authenticate_with_token![:json][:errors]).to eq 'Not authenticated'
end
it 'returns unauthorized status' do
expect(authentication.authenticate_with_token![:status]).to eq :unauthorized
end
end
response
will not be available here, since we are testing the module, not the controller.
When to use a naked class
class Authentication
include Authenticable
end
Authenticable.authenticate_with_token returns error
Failure/Error: allow(authentication).to receive(:render) do |args|
#<Authentication:0xb982078> does not implement: render
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question