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
4
votes
2 answers
Stubbing Grape helper
I have Rails app with Grape API.
The interface is done with Backbone and Grape API provides it all data.
All it returns is user-specific stuff, so i need reference to currently logged in user.
Simplified version looks like this:
API…

pipboy3k
- 123
- 2
- 10
4
votes
1 answer
Rails Test & Mocha: How to stub specific model - conditional any_instance?
I want to stub just a specific model, but not only a specific object and not every instance
E.g. Given class 'Person' with attributes 'name' (string) and 'cool' (boolean). We have two models:
person_bill:
name: bill
cool:…

RngTng
- 1,229
- 2
- 12
- 22
3
votes
4 answers
Mocking/stubbing a method that's included from "instance.extend(DecoratorModule)"
I use a decorator module that get's included in a model instance (through the "extends" method). So for example :
module Decorator
def foo
end
end
class Model < ActiveRecord::Base
end
class ModelsController < ApplicationController
def bar
…

Nikos D
- 431
- 4
- 14
3
votes
2 answers
How to mock Rails::configuration
I'm attempting to test a class which makes use of the rails configuration file. I'd like to mock Rails::configuration.
I've tried things…

Gregory Ostermayr
- 1,123
- 10
- 17
3
votes
3 answers
How do I stub away send_file using mocha
The most direct attempt is to do
@controller.stubs(:send_file)
But that results in an output error like
ActionView::MissingTemplate: Missing template ...
So how do I stub away the send_file method from 2.3.x series.
The question is basically the…

Jarl
- 2,831
- 4
- 24
- 31
3
votes
3 answers
How to make Ruby Mocha mock only check about one parameter
I want to mock this function:
def self.set_segment_info(segment_info, history_record)
history_record.segment_info = segment_info
end
In my test, I want a mock that only confirms that I called set_segment_info with an expected value. I don't…

Reid
- 661
- 1
- 8
- 25
3
votes
1 answer
How to stub a module method inside a controller with Mocha
I have a Sinatra app like this:
my_module.rb
module MyModule
def my_method
"yay"
end
end
app.rb
get "/my_module" do
puts my_method
end
I'm trying to stub my_method on a test with Minitest and mocha.
def test_my_method
…

Matheus Richard
- 654
- 8
- 19
3
votes
0 answers
minitest - expect a method to be called when it was extended from a Module
I am writing a test for the following code:
module Clockwork
every(10.minutes, SomeBackgroundJob)
end
every is an instance method of the Clockwork::Methods module.
In RSpec I can write the following to test that a module method gets…

max pleaner
- 26,189
- 9
- 66
- 118
3
votes
1 answer
How to stub everything on an object using mocha
How to stub out all the methods on an object using mocha ?
I tried
object.stubs(:everything)
stub_everything('class_name')
Both of the above ways are not working.

usha
- 31
- 1
- 2
3
votes
2 answers
How can I appropriately mock out a method that returns yield?
It's fairly common in Ruby for methods that take blocks to look like this:
class File
def open(path, mode)
perform_some_setup
yield
ensure
do_some_teardown
end
end
It's also fairly idiomatic for a method to look like this:
def…

ymbirtt
- 1,481
- 2
- 13
- 24
3
votes
1 answer
Can not run any unit test for Redmine with Ruby 2.0.0 under Windows. Is that at all possible?
Any attempt to run unit tests for Redmine, like:
ruby test/unit/issue_custom_field_test.rb
produces this:
DL is deprecated, please use…

62mkv
- 1,444
- 1
- 16
- 28
3
votes
2 answers
Does a mocked method's code actually run
Hi I'm using Mocha for a Rails project. I'm new to TDD so please forgive me if this is a stupid question.
If I have this
@client.expects(:create_config).once.returns(true)
then am I right in assuming that the code in create_config() won't be run…

robodisco
- 4,162
- 7
- 34
- 48
3
votes
1 answer
use mocha to stub method without any expectation
I am stubbing a method like this in the setup of one of my tests
def setup
super
#blah, blah
GoogleIdentity.stubs(:new).with(google_identity).returns(google_account)
end
The problem is that not every test will invoke the method and…

dagda1
- 26,856
- 59
- 237
- 450
3
votes
1 answer
Test many conditions using mocha
What's the best way to test methods like the following using mocha gem?
def can_create?(user)
opened? && (owner?(user) || member?(user))
end
Is a best practice to test "AND" and "OR" conditions? Should I create many tests to cover all…

Diego Nogueira
- 113
- 5
3
votes
1 answer
How does mocking with Mocha actually allow you to test the logic in a function?
I started out learning how to mock things because using Factory Girl just isn't very practical for projects with 1000+ tests. I can't be hitting the database for every test, especially if I hope to do any sort of automated continuous integration.
My…

NullVoxPopuli
- 61,906
- 73
- 206
- 352