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
2
votes
2 answers

RSpec: use `receive ... exactly ... with ... and_return ...` with different `with` arguments

I'm writing an expectation which checks whether a method is called two times with different arguments and returns different values. At the moment I'm just writing the expectation twice: expect(ctx[:helpers]).to receive(:sanitize_strip).with( …
mdesantis
  • 8,257
  • 4
  • 31
  • 63
2
votes
1 answer

Mocking object that are generated dynamically + rspec-mock

I am using jruby(9.1.12.0) with rspec-rails(3.7.0). I have the following method class AddressCreator include Singleton attr_reader :client def initialize @client = SomeJavaClass.new(some keys go here) end end # SomeJavaClass has…
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
2
votes
1 answer

Stub a 'nested' class method that is called by a wrapper method (RSpec Mocks)

Situation: I want to stub a helper method, so that I can call a method that wraps it and get the stubbed response back. Code is set up like this: class Thing def self.method_one(foo) self.method_two(foo, 'some random string') end def…
tjukes
  • 143
  • 1
  • 10
2
votes
1 answer

Difference between expect and expect_any_instance_of

I have method in my controller that looks like this: def resend_confirmation @current_user.send_confirmation_instructions render json: nil, status: 200 end I've written following spec for that method: require 'rails_helper' describe 'POST…
Mateusz Urbański
  • 7,352
  • 15
  • 68
  • 133
2
votes
1 answer

RSpec spies work differently on two different classes

I have a class Uploader which takes a file and uploads it to S3. I'm trying to test that @s3 is actually receiving a file body when upload_file is called. When I test that File is getting messages sent, the test passes. However, trying to spy on…
RCA
  • 508
  • 4
  • 12
2
votes
1 answer

How to mock message chaining with RSpec?

I have a statement like Model.some_scope.pluck(:a_field) in a method I'd like to test. What's the recommended way for me to stub the return value of this chained method call statement with rspec-mocks 3.4.0? I saw that stub_chain and…
larryzhao
  • 3,173
  • 2
  • 40
  • 62
2
votes
2 answers

Rspec/mocks raises uninitialized constant BasicObject::RSpec

I just installed rspec and rspec-mocks but I am not able to run the simplest setup irb(main):001:0> require 'rspec' => true irb(main):004:0> require 'rspec/mocks' => false irb(main):006:0> RSpec::Mocks::setup(Object.new) NameError: uninitialized…
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
2
votes
1 answer

RSpec: Mocks, Stubs and Verification

I'm writing a plugin for Cinch (the IRC bot), and trying to write some RSpec tests for it. However, I'm trying to get to grips with RSpec, and mocking out the external dependencies of this plugin. I want to test two different things for now - that…
Sam Starling
  • 5,298
  • 3
  • 35
  • 52
1
vote
0 answers

RSpec any_args matcher failing with wrong number of arguments?

I've added the following allow statement to spec_helper.rb to fix an issue in my codebase where tons of specs had added expect().receive on the metrics class making tests brittle to new metrics calls in underlying classes. allow(MetricsClass).to…
Malcolm O'Hare
  • 4,879
  • 3
  • 33
  • 53
1
vote
2 answers

How to best use rspec mocks when using rails mailer parameterization

I'm currently changing our rails mailers to use the newer way of using the mailer that uses parameterization, which brings our code base inline with the rails guide, but more importantly it also allows the parameters to be filtered appropriately in…
Andrew
  • 2,829
  • 3
  • 22
  • 19
1
vote
1 answer

How can I allow a superclass method to return something else?

Given two classes, Superclass and Subclass - How can I use an allow on the Superclass to return something different? #=> given `rspec` is in your $LOAD_PATH, this should be an SSCCE require 'rspec' class Superclass def superclass_method true …
ddavison
  • 28,221
  • 15
  • 85
  • 110
1
vote
1 answer

Expect mock result to receive method

I'm trying to mock a class, so that I can expect it is instantiated and that a certain method is then called. I tried: expect(MyPolicy). to receive(:new). and_wrap_original do |method, *args| expect(method.call(*args)).to…
Jaffa
  • 12,442
  • 4
  • 49
  • 101
1
vote
1 answer

How to check that a method has called another method with RSpec

I have the following code structure (abbreviated for length): module Uploader def self.execute(folder) report_record = Reporting::GenerateReports.call establish_api_connection upload_file(report_record.first_report, folder) end …
user1022788
  • 419
  • 8
  • 18
1
vote
0 answers

Rspec : Declaring a variable that will be created into the method as record

I'm quite new to rails, but I seem to be facing an issue I cannot solve. I am trying to test a background job that will create a new record and use it later to instanciate a new service. As I am testing a background job, it seems that I shoud be…
Matt Flow
  • 11
  • 1
1
vote
3 answers

Verifying rspec mocks before the test is over

It seems like the standard way to use rspec mocks in a test case is to do something like this: class MyTest def setup super ::RSpec::Mocks.setup(self) end def teardown super begin ::RSpec::Mocks.verify ensure …
onlynone
  • 7,602
  • 3
  • 31
  • 50