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
6
votes
2 answers
Mocha : How do you set up an expectation for an instance method?
Assume this ruby code:
class User
def self.failed_login!(email)
user = User.find_by_email(email)
if user
user.failed_login_count = user.failed_login_count + 1
user.save
end
end
end
I want to write a test that tests that…

Jim Soho
- 2,018
- 2
- 21
- 25
6
votes
2 answers
Ruby on Rails: Best way to test a failed call to a third party API
I call a third party web service right now as part of my application. I am using the RestClient gem in order to do this. There are a ton of tools available to do the same thing, so that should not matter.
What I'm curious about is having good enough…

randombits
- 47,058
- 76
- 251
- 433
6
votes
1 answer
NoMethodError: undefined method `mock' with Mocha and Rails 3
I'm trying to use mocha in my Rails 3 project but keep getting the following exception:
NoMethodError: undefined method `mock' for #
…

John Gallagher
- 6,208
- 6
- 40
- 75
6
votes
1 answer
How to stub method with specific parameter (and leave calls with other parameters unstubbed) in Mocha?
This question may seem like a duplicate of this one but the accepted answer does not help with my problem.
Context
Since Rails 5 no longer supports directly manipulating sessions in controller tests (which now inherit from…

Michael Trojanek
- 1,813
- 17
- 15
6
votes
1 answer
How to run Node.js and Ruby tests within one project on Travis CI
I have a repo that contains multiple components, most of them in JavaScript (Node.js) and one written in Ruby (Ruby on Rails). I'd like to have one .travis.yml file that triggers one build that runs all the tests for each of the components.…

matb
- 851
- 13
- 23
6
votes
3 answers
How do I stub a method of an instance only if a specific instance variable has a value?
I have an object MyObject:
class MyObject
def initialize(options = {})
@stat_to_load = options[:stat_to_load] || 'test'
end
def results
[]
end
end
I want to stub the results method only if stat_to_load = "times". How can I do…

Sebastien
- 6,640
- 14
- 57
- 105
6
votes
2 answers
Having never written any automated tests, how should I start behaviour-driven development?
I've been programming for years in plenty of languages and like to think I'm generally pretty good at it. However, I haven't ever written any automated testing: no unit tests, no TDD, no BDD, nothing.
I've tried to start writing proper test suites…

00dani
- 1,498
- 15
- 17
5
votes
1 answer
How to assert block of a mock in mocha
This example is contrived, please don't take it verbatim as my code.
I have the need to assert something like the following:
def mymethod
Dir.chdir('/tmp') do
`ls`
end
end
In the end I want to assert that:
Dir.chdir is invoked with…

Brad
- 109
- 10
5
votes
2 answers
Stubbing Sinatra helper in Cucumber
I am currently struggling with stubbing out a helper method of my Sinatra app from within Cucumber.
I have a Sinatra app with simple session authentication (by cookies) and I want to turn of authentication by stubbing out the logged_in? helper…

Lennart
- 153
- 8
5
votes
2 answers
How to test a mixed-in class method is being called with RSpec and Mocha?
I have a module:
module MyModule
def do_something
# ...
end
end
used by a class as follows:
class MyCommand
extend MyModule
def self.execute
# ...
do_something
end
end
How do I verify that MyCommand.execute calls…

Sébastien Le Callonnec
- 26,254
- 8
- 67
- 80
4
votes
3 answers
Mocking constructors in Ruby
I'm a Java-developer toying with Ruby, and loving it. I have understood that because of Ruby's metaprogramming facilities my unit-tests become much cleaner and I don't need nasty mocking frameworks. I have a class which needs the File class's…

auramo
- 13,167
- 13
- 66
- 88
4
votes
2 answers
IntegrationTest with Mocha, stub HelperMethod (Ruby)
I got a helper method:
has_permission?
In a Module called:
module ApplicationHelper
Inside app/helpers.
I also have an IntegrationTest which includes it:
include ApplicationHelper
My Integration test calls one of my controllers via get…

raycons
- 735
- 12
- 26
4
votes
0 answers
Doing a Spy in a Mocha (Ruby) test?
My team uses Mocha for unit testing in Ruby. I'd like to use Mocha to add a "spy" on a method. That is, I'd like to write a unit test using Mocha that verifies that the code under test calls a particular method on another class, without overriding…

Jon Schneider
- 25,758
- 23
- 142
- 170
4
votes
2 answers
How to mock an instance method of an already mocked object?
I need to mock the following:
Class User
def facebook
#returns an instance of a facebook gem
end
end
So in my User tests, to access the User's facebook info I need to call user.facebook.me.info to retrieve its info. If I want to mock this,…

pjaspers
- 120
- 1
- 7
4
votes
3 answers
Is there a way to undo Mocha stubbing of any_instance?
Within my controller specs I am stubbing out valid? for some routing tests, (based on Ryan Bates nifty_scaffold) as follows :-
it "create action should render new template when model is invalid" do
…

Steve Weet
- 28,126
- 11
- 70
- 86