I'm new to rspec and I'm using RSpec 3.2. I have a method in my controller which is like,
class MainController < ApplicationController
before_filter :get_items
//other methods
def get_items
if @user.admin?
@user_items = [ModuleName].fetchItems
end
if @user_items.empty?
flash.now[:error] = "No items for the user"
render "index"
end
end
end
And my RSpec looks like this,
require "rails_helper"
RSpec.describe MainController, :type => :controller do
describe '#get_items' do
it 'gets the items for admin' do
@controller = MainController.new
@controller.instance_eval{ get_items } # invoke the private method
expect(@controller.instance_eval{ @user_items }).not_to be_blank
expect(response).not_to render_template("index")
end
end
end
The above rspec works fine when I keep @user.admin? as true. How do I test the other 'IF' block where it renders the index and also mock the value of '@user.admin?' to be true or false? Please note that '@user.admin?' comes from another module in my lib folder.
Aucun commentaire:
Enregistrer un commentaire