When you mocked method calls in RSpec, they are mocked until the end of a spec, or until you explicitly...

Rails 7.1 added a new method Rails.env.local?. If you want to stub the Rails env correctly, use ActiveSupport::EnvironmentInquirer like this: # check if the value of stubbed_env is valid...

...allow(Rails).to receive(:env).and_return(ActiveSupport::EnvironmentInquirer.new(stubbed_env.to_s...

github.com

In Selenium features the server and client are running in separate processes. Therefore, when mocking time with a tool like...

Then I should see "Home of Foo" The host will be stubbed for the remainder of one scenario and restored after each scenario (i.e. you do not...

...service model that makes your methods behave like you are on the correct host. Stub app_host to something like foo.example.vcap.me:12345 (vcap.me and any of its subdomains will resolve to...

github.com

By default, Cucumber uses mocha. This note shows to use RSpec stubs and mocks instead. Rspec 1 / Rails 2 Put the following into your env.rb: require 'spec/stubs/cucumber' Rspec 2 / Rails...

...some vintage projects with tests written in Test::Unit instead of RSpec. Mocks and stubs are not features of Test::Unit, but you can use the Mocha gem to add...

...those facilities. The following is a quick crash course to using mocks and stubs in Mocha, written for RSpec users: |---------------------------------------------------------| | RSpec | Mocha | |---------------------------------------------------------| | obj = double() | obj = mock() | | obj.stub(:method => 'value') | obj.stubs...

stackoverflow.com

...to add the following code to your ApplicationController: class ApplicationController < ActionController::Base before_filter :stub_request_ip cattr_accessor :stubbed_request_ip ... private def stub_request_ip if stubbed_ip...

...self.class.stubbed_request_ip request.instance_eval <<-EOS def remote_ip '#{stubbed_ip}' end EOS end end end Please do not waste time to replace the string programming above, I already went...

RSpec 1 (Rails 2) With the most recent spec_candy.rb helpers you can say: User.stub_any_instance(:foo => :bar) user = User.new...

relishapp.com

To only stub a method call if a given argument is used, but use the default implementation for other arguments: object.should_receive(:some_method).and_call_original object.should_receive(:some...

makandra dev

With Rspec you can mock objects or functions, for example like this: expect(my_object).to receive(:my_function).and...

...card with this helper for Cucumber features. Sometimes you feel like you need to stub some CONSTANT you have defined in an other class. Since actually constants are called constants...

...because they're constant, there's no way to easily stub a constant. Here are three solutions for you. Easiest solution Rethink! Do you really need CONSTANT = %w[foo bar...

...response status should be "404" (Optional) In a request spec, you'll need to stub a few config variables (in the cached Rails environment config): # spec/requests/error_pages_spec.rb describe "Error pages" do...

path end end RSpec.configure do |config| config.include CachingHelpers end 3. Now stub the Rails cache and clear it before each example: RSpec.describe SomeClass let(:file_cache) { ActiveSupport...

...Enable caching for individual test (memory store) 1. Leave the default configuration 2. Now stub the Rails cache and clear it before each example: RSpec.describe SomeClass # memory store is per...

...to avoid touching the database for this. For this we used a lot of stubbing and tricks like it_should_run_callbacks. Today we would rather make a few database...

...queries than have a fragile test full of stubs. Example Let's say your User model creates a first Project on creation, so the user doesn't see a blank...

...page that does just that: module InteractWithPage def interact_with_page create_empty_interaction_stub click_empty_interaction_stub ensure remove_empty_interaction_stub end private def create_empty_interaction...

page.execute_script(<<~JS) let stub = document.createElement('div') stub.classList.add('empty-interaction') // Stop the click from bubbling up and closing any overlays stub.addEventListener('click', function(event) { event.stopImmediatePropagation() event.preventDefault() }) // Make sure the...

...drawn within a coordinate system or not: function getPoint(): Point2D | null { // We'll just stub this using randomness return Math.random() > 0.5 ? { x: 1, y: 2 } : null; } Union Types Point2D | null...

github.com

...allows you to record and replay real HTTP responses, saving you the effort to stub out request/response cycles in close details. If your tests do require close inspection of requests...

...shares many concepts with WebMock. You can fake a remote server response like this: stub_request(:get, 'http://host/api').to_return(:body => 'fake body') You can test that a request...

thegnar.com

...Helpers used within your view are not included by default. Thus, you may to stub them or include all helpers of a controller on purpose. There is also an option...

...use Webmock or VCR for the lib/github_client part, but your application only talks to stubs or adapters and never know anything about the API internals. It feels more natural, that...

...do the parsing of JSON by default. If it is not an option to stub the cookies jar object, you can create one by using: cookies_jar = ActionDispatch::Cookies::CookieJar.build...

...encrypted won't work within the model context on cookies_jar and will require stubbing these method or having to assure some of the cookies dependencies. Helper Specs Setting cookies...

Be careful when stubbing out attributes on records that are defined by associations. Nothing is as it seems to be. The associated record has its own universe of things; when...

...delegating calls to it, you ca not stub methods on the associated record and expect them to be around. That is a general issue with this pattern/approach. What's happening...

makandra dev
web.archive.org

When testing your command line application with Aruba, you might need to stub out other binaries you don't want to be invoked by your test. Aruba Doubles is a...

Consider the following: describe '#something' do context 'with lots of required arguments' do it 'should work' do subject.something(:foo => 'foo...

...example.com/?foo[0]=1&foo[1]=2&foo[2]=3 RestClient: http://example.com/?foo[]=1&foo[]=2&foo[]=3 Webmock normalizes this url when matching to your stubs, so it is always http://example.com/?foo[]=1&foo[]=2&foo[]=3. This might lead to green tests, but in...