RSpec: How to disable Sorcery method call in controller?

Controller:

class SessionsController < ApplicationController layout 'login' def create user = login(params[:username], params[:password]) if user redirect_back_or_to root_url else flash.now.alert = "Username or password was invalid" render :new end end end 

Test:

 require 'spec_helper' describe SessionsController do describe "POST create" do before(:each) { Fabricate(:school) } it "Should log me in" do post(:create, {'password' => 'Secret', 'username' => 'director'}) response.should redirect_to('/') end end end 

Fabricate(:school) has a callback that generates the first user. I want to reorganize this code so that it does not use database calls at all. I want to mute the login, so that it will return true.

How can I stub the login method? It comes from Magic.

https://github.com/NoamB/sorcery/blob/master/lib/sorcery/controller.rb#L31

+4
source share
1 answer

Since the Sorcery login method is added as an instance method for the controller, you must mock this method on the current controller instance, that is, "@controller". See http://api.rubyonrails.org/classes/ActionController/TestCase.html .

With Flexmock:

 flexmock(@controller).should_receive(:login).and_return(flexmock('user')).once 

Or RSpec mocks:

 @controller.stub(:login) { double "user" } 
+3
source

Source: https://habr.com/ru/post/1380650/


All Articles