The tricky thing about register_url(@user.perishable_token) is that the receiver is self (in whatever context this is taken from) and without seeing the fuller context to know what self is there, I can’t provide an exact snippet that will work to stub that. If it’s an object that you don’t have a reference to in your test (e.g. because rails instantiates it for you) then you’re going to have even more difficulty stubbing it.
Instead, my recommendation is to put this logic behind and interface; the you can define a second, polymorphic implementation of it for your test environment. Something like:
# in lib/account_activation_link_generator.rb or similar
class AccountActivationLinkGenerator
include Rails.application.routes.url_helpers
def generate_for(user)
register_url(user.perishable_token)
end
end
# in config/application.rb:
require 'account_activation_link_generator'
config.account_activation_link_generator = AccountActivationLinkGenerator.new
# in spec/rails_helper.rb or spec/spec_helper.rb (depending on your test env setup)
class TestAccountActivationLinkGenerator
def generate_for(user)
# return a static url for your test environment
end
end
Rails.application.config.account_activation_link_generator = TestAccountActivationLinkGenerator.new