Questions tagged [rspec-mocks]

rspec-mocks is the part of the RSpec testing framework that provides test doubles: stubs, mocks and spies.

rspec-mocks is the part of the RSpec testing framework that provides test doubles: stubs, mocks and spies. It can be used with other test frameworks such as Cucumber (although it is only occasionally appropriate to use test doubles in Cucumber scenarios). It is implemented as a Ruby gem.

65 questions
1
vote
1 answer

How to spy just one call of ActiveSupport::Notifications #instrument, not all of them

I'm making a Rspec test that checks if ActiveSupport::Notification.instrument was called with some parameters. The thing is that in order to make this test a need FactoryBot to build some objects, but when I try to spy on…
Caio Salgado
  • 307
  • 2
  • 9
1
vote
2 answers

Rspec-mocks doesn't raise the exception

I have this piece of RoR code that creates a gitlab repository. If the repository already exists, the method returns false with the error message. class CreateRepositoryJob < ApplicationJob queue_as :default def perform(id) namespace =…
Yann PRONO
  • 203
  • 1
  • 2
  • 9
1
vote
1 answer

Is it possible to expect an RSpec double be called twice with varying values?

On RSpec's mock docs I found expect(double).to receive(:msg).exactly(3).times.and_return(value1, value2, value3) # returns value1 the first time, value2 the second, etc` If I do the same with parameters, for ex. expect(double).to…
1
vote
1 answer

How to stub/mock without coupling the test to the code under test with rspec?

Say there's a worker, whose job is to: find or create a record by a set of critaria; update an attribute of the record. Here's a sample implementation: class HardWorker include SidekiqWorker def perform(foo_id, bar_id) record =…
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
1
vote
2 answers

Write rspec for rake task

i have task /lib/crawler.rake like that: namespace :crawler do area_names = Dir[Rails.root.join("lib", "crawler", "*.rb")].map do |file_name| File.basename(file_name, ".rb") end area_names.each do |area_name| task area_name.to_sym =>…
Tai Tri Vo
  • 55
  • 1
  • 8
1
vote
1 answer

expect(Class).to receive(:x).with(hash_including(y: :z)) doesn't work

I want to check that Pandoc.convert is called with to: :docx option like this: options = {to: :docx} PandocRuby.convert("some string", options) I have the following expectation in a spec: expect(PandocRuby).to…
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
1
vote
2 answers

How to make rspec-mocks' expect to receive.with fail eagerly

rspec-mocks' expect(target).to receive(:message).with(arg_matcher) will only show an error at the end of the test if the target is invoked with parameters not matching the arg matcher passed to with. Is there a way to force it to fail eagerly -…
astgtciv
  • 720
  • 5
  • 15
1
vote
1 answer

In RSpec, how can I mock a method called with a constant argument?

I'm creating a gem that will have a constant in whatever app it is being used in (after a generator is run), but that constant doesn't exist in the gem itself. I'm having an issue with RSpec inside the gem not skipping over the part of code that is…
xeroshogun
  • 1,062
  • 1
  • 18
  • 31
1
vote
1 answer

How to stub Searchkick in search method in controller

I am new to RSpec. I want to test my search controller. I try to stub my search method in the controller but it always fails. Failure/Error: expect(Doctor).to receive(:search).with(search_param) (
1
vote
2 answers

How do I test the rescue block of a method with rspec mocks 3.3

Help me make this test pass: Here is an example of some rspec code, class User attr_accessor :count def initialize @count = 0 end # sometimes raises def danger puts "IO can be dangerous..." rescue IOError => e @count +=…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
1
vote
1 answer

Undefined method `i18n_scope' for RSpec::Mocks::Mock:Class

I recently upgraded to Rails 4. When I ran my RSpec tests I got an error. Here is my spec: 1 model = double(:model, errors: double(:errors, full_messages: [])) 2 transaction = double(:transaction) 3 transaction.stub(:update_attributes!) {…
1
vote
1 answer

RSpec.configure not working and examples fails due to rspec-mocks#setup

I'am started rspec 3 months ago and i'am working with it in a sinatra project (using ruby 2.1.1) ... I don't have errors in my specs (they're successfully running in other computers) but while running them on my own i got this for every single…
atacraft
  • 25
  • 1
  • 7
1
vote
1 answer

What the benefit of FactoryGirl over Rspec-Mocks in controller and view specs?

In an outside-in Rails development approach,we start from the views and walk through the models, in this approach we specify and test each part in isolation so that view specs are isolated from controllers and models, controller specs are isolated…
Nafaa Boutefer
  • 2,169
  • 19
  • 26
1
vote
1 answer

rspec: "no such file to load -- rspec/mocks"

I'm trying to run a test on some very simple Ruby. I have ruby and rails installed on my linux machine. I also have rspec installed. My gem list is: LOCAL GEMS actionmailer (3.2.8) actionpack (3.2.8) activemodel (3.2.8) activerecord…
tir38
  • 9,810
  • 10
  • 64
  • 107
1
vote
1 answer

rspec error - no such file to load -- rspec/mocks (LoadError)

I am learning rspec. I have the simple example below. Why am I getting /usr/lib/ruby/vendor_ruby/rspec/core/mocking/ with_rspec.rb:1:in `require': no such file to load -- rspec/mocks (LoadError) with describe "It should return the parameter that…
junky
  • 1,480
  • 1
  • 17
  • 32