How to know the flow of the controller method using Rspec - ruby-on-rails

I have two dependent drop down.One gives me orgname and other drop down populates on selecting a orgname, That is teamname.
This is my github_leader_board_spec.rb
describe "github_leader_board" do
before do
#obj = DashboardsController.new
end
context "with session" do
subject { get :github_leader_board, :params => { :orgname => "test", :teamname=> "team"}}
it "returns http success" do
expect(response).to have_http_status(:success)
end
it "executes other functions" do
expect(#org_data).not_to be_nil
expect(#obj.get_team_api("DevCenter")).not_to be_nil
end
end
end
This is my controller method
def github_leader_board
myhash = {}
#points_hash = {}
member_data = []
#org_data = get_org_api
#orgs = get_names(org_data)
team_data = get_team_api(params[:orgname])
#teams = get_names(team_data)
teamid = get_team_id(team_data)
#teams.each_with_index {|k,i|myhash[k] = teamid[i]}
myhash.each do |key,value|
if key == params[:teamname]
member_data = get_members("#{value}")
end
end
#memberids = get_names(member_data)
member_names = get_member_names(#memberids)
review_comments = get_reviewcoments(#memberids)
reactions = points(#memberids)
points = [review_comments, reactions].transpose.map {|x| x.reduce(:+)}
member_names.each_with_index {|k,i|#points_hash[k] = points[i]}
end
If i run my spec file it says, undefined #org_data. The function inside the github_leader_board controller is not calling the get_org_api and storing the value to the #org_data variable.
Can anybody suggest what is wrong with the code and how can i improve it. As i'm new to ror.
Any help would be appreciated.
Thank you.

I believe you could use a test of the type controller, instead of instantiating your controller and then use the RSpec method assigns (docs) to test your instance variables, something like this:
RSpec.describe DashboardsController, :type => :controller do
context "with session" do
# ...
it "executes other functions" do
expect(assigns(:org_data)).not_to be_nil
end
end
end
https://relishapp.com/rspec/rspec-rails/docs/controller-specs
Also, if you want to check the flow, and debug your code, you can use the gems pry, pry-rails and pry-nav as #Marek Lipka stated.

Related

Rspec: Mock recaptcha verification

I am trying to create a request spec for a form submission and my recaptcha verification is causing the test to fail. I have a pretty simple test:
RSpec.describe "PotentialClients", type: :request do
let(:pc_attributes) { ... }
describe "POST /potential_clients" do
it "should create record" do
expect { post potential_clients_path, params: { potential_client: pc_attributes } }
.to change(PotentialClient, :count).by(+1)
end
end
end
I run into an issue because in PotentialClients#create I make a call to verify_recaptcha? which returns false in the test instead of true:
# potential_clients_controller.rb
def create
#potential_client = PotentialClient.new(potential_client_params)
page_success = verify_recaptcha?(params[:recaptcha_token], 'lead_creation_page')
if page_success && #potential_client.save
...
end
end
# application_controller.rb
def verify_recaptcha?(token, recaptcha_action)
secret_key = ENV['CAPTCHA_SECRET_KEY']
uri = URI.parse("https://www.google.com/recaptcha/api/siteverify?secret=#{secret_key}&response=#{token}")
response = Net::HTTP.get_response(uri)
json = JSON.parse(response.body)
if json['success'] && json['score'] > RECAPTCHA_MINIMUM_SCORE && (json['action'] == "lead_creation_page" || json['action'] == "lead_creation_modal")
return true
elsif json['success'] == false && json["error-codes"].include?("timeout-or-duplicate")
return true
end
return false
end
How should I mock the call to verify_recapthca? so that my test passes? I've tried:
allow(PotentialClient).to receive(:verify_recaptcha?).and_return(true)
# and
allow_any_instance_of(PotentialClient).to receive(:verify_recaptcha?).and_return(true)
but both threw errors:
PotentialClient(...) does not implement: verify_recaptcha?
allow(PotentialClient).to receive(:verify_recaptcha?).and_return(true)
This isn't working because—as the error message says—PotentialClient (the model) doesn't have a method called verify_recaptcha?. The method is defined in ApplicationController, which is extended by PotentialClientsController, and that's where you need to mock it.
My Rails is rusty, but it looks like in an rspec-rails controller spec the current instance of the controller is exposed by the controller method. In that case, what you want is this:
allow_any_instance_of(ApplicationController).to receive(:verify_recaptcha?).and_return(true)

How do I test with Rspec an initialize method that invokes find_or_initialize_by?

How do I write a test to check for the find_or_initialize_by block in this initialize method?
def initialize(document, user)
#document = document
#api = #document.api_version
#template = #document.template
#user = user
#brand = #user.brand
#vars = master_var_hash.extend Hashie::Extensions::DeepFind
template_variables.each do |t|
#document.template_variables.find_or_initialize_by(name: t.name) do |d|
d.name = t.name
d.tag = t.tag
d.box_name = t.box_name
d.designator = t.designator
d.order_index = t.order_index
d.master_id = t.id
d.editable = t.editable
d.editable_title = t.editable_title
d.html_box = t.box.stack.html_box if #api == :v3
d.text = t.name == 'title' ? default_title : user_value(t)
end
end
end
I want to be able to test that the right values have been assigned to the #document's TemplateVariables from the class' TemplateVariables. In my coverage report I can't even hit inside the find_or_initialize_by block.
My test for size doesn't really check what I want to test here:
describe 'template_variables' do
it 'initializes all the new vars per document' do
expect(document.template_variables.size).to eq subject.master_var_hash.size
end
end
How can I write a test to check all those values and cover those lines?
You could use
expect(document).to receive_message_chain(:template_variables, :find_or_initialize_by).exactly(8).times
but it's a mess and you would have to also check if each call got proper parameters.
I would suggest extracting this to a method:
Document#initialize_variables(template_variables)
then you could test it as simply as
expect(document).to receive(:initialize_variables).with(expected_hash)
and then you can cover Document#initialize_variables with specs and test it's behavior in depth.

Rspec passing view params to controller spec

My problem is I am trying to get as much coverage over my methods using rspec and I am unable to test a few certain lines. I am trying to pass a params hash to my controller method in my rspec to simulate the values from the view. Essentially, these values will filter results to be displayed on my index page.
The controller method I am testing is:
def index
#buildings = Building.all
#buildings = #buildings.searchaddress(params[:searchaddress])
if params[:searchcompany] != nil
#buildings = #buildings.searchcompany(params[:searchcompany][:management])
end
if params[:searchcompany] != nil
#buildings = #buildings.searchcity(params[:searchcity][:city])
end
if params[:searchparking] == 'on'
params[:searchparking] = 't'
#buildings = #buildings.searchparking(params[:searchparking])
end
if params[:searchpets] != nil
params[:searchpets] = 't'
#buildings = #buildings.searchpets(params[:searchpets])
end
end
I am trying to pass the params hash in my rspec test. I have tried a few ways including this one:
describe "viewing all buildings" do
it "renders index template" do
param = Hash.new()
param[:searchcompany] = [management:'asdf']
param[:searchcity] = [city:'asdf'] #have also tried {city:""}
param[:searchparking] = ['on']
param[:searchpets] = [true]
param[:searchaddress] = ['lalala']
get :index, params:param #{searchcompany:{management:'asdf'}, searchcity:{city:'asdf'}, searchparking:'on', searchpets:true}
expect(response).to render_template('index')
expect(assigns(:buildings)).to be_truthy
expect(Building).to receive(Building.searchcompany)
expect(Building).to receive(Building.searchcity)
expect(Building).to receive(Building.searchpets)
expect(Building).to receive(Building.searchparking)
end
end
The searchpets, searchcompany, etc. methods are from my Building model and are implemented as
def self.searchaddress(search)
where("address LIKE ?", "%#{search}%")
end
Here is the error I am getting:
1) BuildingsController viewing all buildings renders index template
Failure/Error:
def self.searchcity(search)
where("city LIKE ?", "%#{search}%")
ArgumentError:
wrong number of arguments (0 for 1)
# ./app/models/building.rb:39:in `searchcity'
# ./spec/controllers/buildings_controller_spec.rb:103:in `block (3 levels) in <top (required)>'
How do i pass [:searchcity][:city] to my controller method through my rspec test?
You could try:
it "renders index template" do
params = {
searchcompany: {
management: 'asdf'
},
searchcity: {
city: 'asdf'
},
searchparking: 'on'
}
# All your expectations like 'expect(sth).to receive(:method)' go here
get :index, params
# Your remaining expectations go here
end

How to test a specific line in a rails model using rspec

I have a model with an initializer in it, which basically creates a user from a user hash.
After it gets the user information, it checks whether the "privileges" key in the hash is an array. If it's not, it turns it into an array.
Now the obvious way of doing this would be crafting an entire user_hash so that it would skip those "create user" lines and then check if it turns the input into an array if necessary. However, I was wondering if there is a more DRY way of doing this?
Here is the user model I'm talking about:
def initialize(opts={})
#first_name = opts[:user_hash][:first]
#last_name = opts[:user_hash][:last]
#user_name = opts[:user_hash][:user_name]
#email = opts[:user_hash][:email]
#user_id = opts[:user_hash][:id]
#privileges = {}
if opts[:privs].present?
if !opts[:privs].kind_of?(Array)
opts[:privs] = [opts[:privs]]
end
end
end
You can pass a double which returns the needed value when the proper key is requested, and itself (or something else) otherwise:
it 'turns privs into an array' do
opts = double(:opts)
allow(opts)to receive(:[]).and_return(opts)
allow(opts)to receive(:[]).with(:privs).and_return('not array')
expect(MyClass.new(opts).privileges).to eq(['not array'])
end
Btw, your code could be simplified using the splat operator:
privs = [*opts[:privs]]
sample behavior:
privs = nil
[*privs]
# => []
privs = ['my', 'array']
[*privs]
# => ["my", "array"]
privs = 'my array'
[*privs]
# => ["my array"]
You can even use the idempotent Kernel#Array
def initialize(opts = {})
#first_name = opts[:user_hash][:first]
#last_name = opts[:user_hash][:last]
#user_name = opts[:user_hash][:user_name]
#email = opts[:user_hash][:email]
#user_id = opts[:user_hash][:id]
#privileges = {}
Array(opts[:privs])
end
I hope that helps
Rather than testing the implementation (value is turned into an array), I would test the desired behavior (takes single privilege or multiple privileges):
describe User do
describe '#initialize' do
it "takes single privilege" do
user = User.new(user_hash: {}, privs: 'foo')
expect(user.privileges).to eq(['foo'])
end
it "takes multiple privileges" do
user = User.new(user_hash: {}, privs: ['foo', 'bar'])
expect(user.privileges).to eq(['foo', 'bar'])
end
end
end

Refactoring Rspec specs

I am trying to cleanup my specs as they are becoming extremely repetitive.
I have the following spec
describe "Countries API" do
it "should render a country list" do
co1 = Factory(:country)
co2 = Factory(:country)
result = invoke :GetCountryList, "empty_auth"
result.should be_an_instance_of(Api::GetCountryListReply)
result.status.should be_an_instance_of(Api::SoapStatus)
result.status.code.should eql 0
result.status.errors.should be_an_instance_of Array
result.status.errors.length.should eql 0
result.country_list.should be_an_instance_of Array
result.country_list.first.should be_an_instance_of(Api::Country)
result.country_list.should have(2).items
end
it_should_behave_like "All Web Services"
it "should render a non-zero status for an invalid request"
end
The block of code that checks the status will appear in all of my specs for 50-60 APIs. My first thought was to move that to a method and that refactoring certainly makes things much drier as follows :-
def status_should_be_valid(status)
status.should be_an_instance_of(Api::SoapStatus)
status.code.should eql 0
status.errors.should be_an_instance_of Array
status.errors.length.should eql 0
end
describe "Countries API" do
it "should render a country list" do
co1 = Factory(:country)
co2 = Factory(:country)
result = invoke :GetCountryList, "empty_auth"
result.should be_an_instance_of(Api::GetCountryListReply)
status_should_be_valid(result.status)
result.country_list.should be_an_instance_of Array
result.country_list.first.should be_an_instance_of(Api::Country)
result.country_list.should have(2).items
end
end
This works however I can not help feeling that this is not the "right" way to do it and I should be using shared specs, however looking at the method for defining shared specs I can not easily see how I would refactor this example to use a shared spec.
How would I do this with shared specs and without having to re-run the relatively costly block at the beginning namely
co1 = Factory(:country)
co2 = Factory(:country)
result = invoke :GetCountryList, "empty_auth"
Here's one option, using the new-ish "subject" feature of RSpec. Note that this will run the before :all block twice, once for each nested "describe" block. If this ends up being too slow, you can flatten things out at the cost of not being able to use the "subject" syntax for the status shared examples (since subject applies to the entire describe block it's used in).
shared_examples_for "valid status" do
it { should be_an_instance_of(Api::SoapStatus) }
its(:code) { should eql(0) }
its(:errors) { should be_an_instance_of(Array) }
its(:errors) { should be_empty }
end
describe "Countries API" do
before :all do
co1 = Factory(:country)
co2 = Factory(:country)
#result = invoke :GetCountryList, "empty_auth"
end
subject { #result }
it { should be_an_instance_of(Api::GetCountryListReply) }
its(:country_list) { should be_an_instance_of (Array) }
it "should have countries in the country list" do
#result.country_list.each {|c| c.should be_an_instance_of(Api::Country)}
end
its(:country_list) { should have(2).items }
describe "result status" do
subject { #result.status }
it_should_behave_like "valid status"
end
end

Resources