Reputation: 853
I have
class Api::V1::BaseController < ApplicationController
before_filter :authenticate!
private
def authenticate!
session_token = request.env["HTTP_SESSIONTOKEN"]
@current_user = User.where(session_token: session_token).first unless session_token.blank?
unless @current_user
return render_json({message: "ERROR: Invalid Session"})
end
end
def current_user
@current_user
end
end
I am testing session_controller which inherits base_controller
before do
post :create, {email: "[email protected]", user_name: "raj",password: "raj"}
body = JSON.parse(response.body)
@session_token = body["session_token"]
end
describe "PUT #edit_email" do
context "if new email already exist" do
it "should return with a error message" do
put :edit_email, {email: "[email protected]", new_email: "[email protected]", password: "raj"}
body = JSON.parse(response.body)
expect(body["message"]).to eq("email already exist")
end
end
end
I am new to rspec and here I am confused about calling private authenticate method with session_token. How to call private method of controller and pass header.Thanks in advance.
Upvotes: 1
Views: 2267
Reputation: 7540
You can set your env variable like following:
request.env['HTTP_SESSIONTOKEN'] = "..."
For testing your private method:
controller.instance_eval{ authenticate }.should eql ...
Upvotes: 2
Reputation: 52357
In Ruby/Rails and OOP in general private methods should not be tested. You should test the interface, not the implementation of private method.
Implementation could be changed with time - but the interface it provides would (most likely) not.
Upvotes: 4