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
41
votes
5 answers

RSpec Mock Object Example

I am new to mock objects, and I am trying to learn how to use them in RSpec. Can someone please post an example (a hello RSpec Mock object world type example), or a link (or any other reference) on how to use the RSpec mock object API?
ab217
  • 16,900
  • 25
  • 74
  • 92
22
votes
2 answers

Rspec - combine expect_any_instance_of and a receive counts

I need to verify that any instance of my class receives a certain method, but I don't care if many instances receive it (they're supposed to). I tried like this: expect_any_instance_of(MyClass).to receive(:my_method).at_least(:once) But apparently,…
Mat
  • 952
  • 2
  • 11
  • 28
18
votes
4 answers

rspec-mocks' doubles are designed to only last for one example

I've got a question about how to share rspec-mocks' double between examples. I'm writing a new rails app with rspec-mocks 3.1.3. I'm used to using the old (< 2.14 and and trying to update my knowledge if current rspec usage. I have a model…
daveharris
  • 285
  • 1
  • 2
  • 9
14
votes
2 answers

How to avoid using allow_any_instance_of?

Imagine we have following piece of code: class A def create_server options = { name: NameBuilder.new.build_name } do_some_operations(options) end end To test such methods, I've used to use allow_any_instance_of: it 'does…
hedgesky
  • 3,271
  • 1
  • 21
  • 36
12
votes
2 answers

How to use sorbet type checking with RSpec mocks?

I have a method that that has a sorbet type signature definition. While trying to mock this method in tests using RSpec I get a type mismatch error. I'm trying to understand how I can resolve this issue and can add RSpec based tests without…
Rahil Shah
  • 183
  • 1
  • 4
10
votes
3 answers

Rspec mocks, can 'expect' also stub a method as a side effect?

I'm trying to make sense of the tests in an inherited app, and I need some help. There are lots of spec groups like this one (view spec): let(:job_post) { FactoryGirl.create(:job_post) } # ... before do expect(view).to…
tompave
  • 11,952
  • 7
  • 37
  • 63
9
votes
1 answer

How to stub a class method using rspec/rspec-mocks

I am using rspec-mock for test-driven-development. I am starting implementing a single class and mocking/stubbing the other classes using rspec-mock. Mocking objects of classes yet to be implemented works well. However when I try to mock a class…
Juergen
  • 115
  • 1
  • 1
  • 5
8
votes
4 answers

RSpec mocking an :each block

I want to use RSpec mocks to provide canned input to a block. Ruby: class Parser attr_accessor :extracted def parse(fname) File.open(fname).each do |line| extracted = line if line =~ /^RCS file: (.*),v$/ end …
Evgeny
  • 6,533
  • 5
  • 58
  • 64
8
votes
1 answer

undefined method instance_double for RSpec::Mocks::ExampleMethods

I have a test case like this: describe WorkCardsController do it "something" do work_card = instance_double(WorkCard, {:started?=>true} ) #some more code end end When I run RSpec, I get an error: undefined method…
Jacek
  • 81
  • 1
  • 4
7
votes
0 answers

What are the pros and cons of rspec mocking versus other mocking frameworks?

I have seen a lot of outdated podcasts that mention mocha as a gem I would want to install because it does mocking better then rspec. I have a feeling that the rspec developers have caught on to this and have improved their mocking since then.…
aarona
  • 35,986
  • 41
  • 138
  • 186
5
votes
1 answer

rspec-mocks 'allow' returns undefined method

I'm using RSpec2 v2.13.1 and it seems that rspec-mocks (https://github.com/rspec/rspec-mocks) should be included in it. Certainly it's listed in my Gemfile.lock. However, when I run my tests I get Failure/Error: allow(Notifier).to…
4
votes
2 answers

How can I supress warning "removing `initialize' may cause serious problems" in rspec?

I have a test that does: allow_any_instance_of(GoogleMapsService::Client).to receive(:initialize) and I'm getting warning: removing 'initialize' may cause serious problems, but I didn't find any other way to stub this. How can I solve it in another…
Leticia Esperon
  • 2,499
  • 1
  • 18
  • 40
4
votes
1 answer

How do I completely avoid using a database in RSpec tests?

I want to use FactoryGirl to build in-memory stubs of models, then have all ActiveRecord queries run against only those. For example: # Assume we start with an empty database, a Foo model, # and a Foo factory definition. #foo_spec.rb stubbed_foo…
3
votes
2 answers

RSpec - How to properly use doubles and stub methods on helper objects?

An action of a Rails controller makes an instance of a helper class (say SomeService), which performs some work and returns a results, something along the lines of: def create ... result = SomeService.new.process ... end I want to stub what…
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
3
votes
1 answer

How to test if a class receives a message in RSpec?

I have a service object something like: class SecurityQueryService class Contract::NotFound < StandardError; end attr_reader :ticker_name, :contract_type, :contract def initialize(user, params) @user = user @ticker_name =…
Eki Eqbal
  • 5,779
  • 9
  • 47
  • 81
1
2 3 4 5