shakacode/cypress-on-rails

Mock/stub rails method

deemaagog opened this issue · 1 comments

Hi! At first thanks a lot for that awesome library!

Rails newbie here. I have a method in my application

def direct_request?
  // logic here
end

Usually it returns false, but I want it to be true in some scenarios when I call cy.visit().
I tried to create command with following code
Dashboard::BaseController.any_instance.stubs(:direct_request?).returns(true) and call it in my test cy.app('set_direct_request') but it doesn't work.
Is it possible to mock/stub it ? Thanks in advance!

Unfortunately, currently there is no way to stub methods 😿

It's a pretty tricky thing to do and hopefully someone one day will figure it out 🙏🏽

In the meantime you could look at using a "flag"

def direct_request?
  return true if ENV['SKIP_DIRECT_REQUESTS']
  // logic here
end

set_direct_request.rb

  ENV['SKIP_DIRECT_REQUESTS'] = command_options['skip']

in cypress

  // enable
  cy.app('set_direct_request', { skip: true } )

  // disable
  cy.app('set_direct_request', { skip: false } )