Mocha is a Ruby library for mocking and unit testing. For the JS library use [mocha.js]. Use with [ruby] or [ruby-on-rails]
Questions tagged [ruby-mocha]
114 questions
33
votes
3 answers
Is there a way to undo Mocha stubbing of any_instance in Test::Unit
Much like this question, I too am using Ryan Bates's nifty_scaffold. It has the desirable aspect of using Mocha's any_instance method to force an "invalid" state in model objects buried behind the controller.
Unlike the question I linked to, I'm not…

Craig Walker
- 49,871
- 54
- 152
- 212
21
votes
1 answer
Ruby Mocha: is there an equivalent to rspec-mocks' #and_call_original?
Rspec-mocks has expect(some_object).to receive(:some_method).and_call_original. Can I do this with Mocha, and if so, how? some_object.expects(:some_method).... ...what?

L2G
- 846
- 6
- 28
18
votes
6 answers
How do I allow Chrome to use my microphone programmatically?
I am currently trying to run some tests made with webdriverjs and chromedriver but they need microphone permissions.
This is the popup that shows up:
I have tried:
chromedriver.start(['--disable-popup-blocking']);
driver = new…

Tiago Bértolo
- 3,874
- 3
- 35
- 53
17
votes
5 answers
Mocha Mock Carries To Another Test
I have been following the 15 TDD steps to create a Rails application guide - but have run into an issue I cannot seem to resolve. For the functional test of the WordsController, I have the following code:
class WordsControllerTest <…

MunkiPhD
- 3,636
- 1
- 29
- 51
16
votes
3 answers
Mocha: How to add expectation of a method when there are multiple invocations with different parameters
I have a Rails controller action to test. In that action, a method User.can? is invoked several times with different parameters. In one of the test case for it, I want to make sure that User.can?('withdraw') is invoked. But I don't care about…

Innerpeacer
- 1,321
- 12
- 20
16
votes
4 answers
Is there a "not_expects" for mocha/rspec?
I need to make sure a method is not called giving a specific set of conditions, and I'm
looking for the opposite of the mocha expects.

rafamvc
- 8,227
- 6
- 31
- 34
15
votes
4 answers
How to return a dynamic value from a Mocha mock in Ruby
The gist of my problem is as follows:-
I'm writing a Mocha mock in Ruby for the method represented as "post_to_embassy" below. It is not really our concern, for the purpose of describing the problem, what the actual method does. But I need the mock…

Vivek
- 151
- 1
- 1
- 4
13
votes
1 answer
Set expectation of method call while still calling original implementation
It appears as though setting any method-call expectation with Mocha prevent the original implementation from being called.
This seems to cover calling the original method with rspec.
Is there a way to do this with Mocha? Or does anyone know why…

aceofspades
- 7,568
- 1
- 35
- 48
13
votes
1 answer
getting the object passed as an argument to a stubbed method with Mocha
Foo.expects(:bar)
Foo.bar(:abc => 123, :xyz => 987)
# assert Foo.bar was called with a hash that has a key of :abc == 123
Basically I want to examine the object passed as an argument to a stubbed method, in order to inspect on a value of that…

Alex Wayne
- 178,991
- 47
- 309
- 337
13
votes
1 answer
Getting Rails 3 Generators with Rspec 2 and Mocha
I've followed all of the steps that I've been able to find online for configuring Rails 3 with Rspec 2 and Mocha. In my Gemfile:
group :development do
gem 'rails3-generators'
gem "rspec", '>= 2.0.0.beta.19'
gem "rspec-rails", '>=…

Corey
- 231
- 1
- 5
11
votes
1 answer
Is there a Mocha equivalent of Rspec’s “mock().as_null_object”?
Is there a Mocha equivalent of Rspec’s “mock().as_null_object”?

Evan
- 271
- 1
- 6
10
votes
1 answer
using mocha, is there a way to stub with many parameters?
Let's assume that i have this class
class Foo
def bar(param1=nil, param2=nil, param3=nil)
:bar1 if param1
:bar2 if param2
:bar3 if param3
end
end
I can stub whole bar method…

allenhwkim
- 27,270
- 18
- 89
- 122
10
votes
3 answers
Is it possible to stub a method in a parent class so that all subclass instances are stubbed in rspec?
Given a parent class Fruit and its subclasses Apple and Banana, is it possible to stub the method foo defined in Fruit, so that any calls to method foo on any instances of Apple and Banana are stubbed?
class Fruit
def foo
puts "some magic in…

Innerpeacer
- 1,321
- 12
- 20
9
votes
3 answers
Is there a way to set the value of $? in a mock in Ruby?
I am testing some scripts that interface with system commands. Their logic depends on the return code of the system commands, i.e. the value of $?. So, as a simplified example, the script might say:
def foo(command)
output=`#{command}`
if $?==0
…

rleber
- 573
- 3
- 9
7
votes
3 answers
Mock methods that receives a block as parameter
I have a scenario more or less like this
class A
def initialize(&block)
b = B.new(&block)
end
end
I am unit testing class A and I want to know if B#new is receiving the block passed to A#new. I am using Mocha as mock framework.
Is it…

rafaelss
- 81
- 1
- 5