Questions tagged [ruby-mocha]

Mocha is a Ruby library for mocking and unit testing. For the JS library use [mocha.js]. Use with [ruby] or [ruby-on-rails]

114 questions
1
vote
2 answers

mocha and nested objects

Excuse if this is a silly question, I am new to mocking. I am able to use mocha to do things like: person.expects(:first_name).returns('David') How can I mock a nested object? Say I have a Product that belongs to a Person and I want to get the…
user138095
1
vote
1 answer

stub is not defined while testing

Suppose, I am running this test Class MyModelTest < ActiveSupport::TestCase def setup do @mymodel = MyModel.new end @mymodel.stub(:method).and_return { true } but I get :undefined method 'stub' for nil:NilClass. I thought it was…
user1611830
  • 4,749
  • 10
  • 52
  • 89
1
vote
1 answer

How to mock an actual Object#method call using Mocha in Ruby?

Interacting directly with brains is not easy, so I have a little Gateway Pattern in use with some Dependency Inversion. NumberCruncher is a wrapper for my Brain class. class NumberCruncher def initialize brain = Brain.new @brain = brain …
Mulan
  • 129,518
  • 31
  • 228
  • 259
1
vote
1 answer

How to test factory pattern using Mocha in Ruby?

I'm having trouble getting this simple mocha test to work Diclaimer I'm new to mocha! My Factory class # lib/fabtory.rb class Factory def self.build klass, *args klass.new *args end end My test code # test/test_factory.rb class FactoryTest…
Mulan
  • 129,518
  • 31
  • 228
  • 259
1
vote
1 answer

Mocha expected at most once, invoked twice, but method is clearly invoked only once

I'm using Mocha for mock testing. Below is the relevant code: # test_player.rb should "not download the ppg more than once for a given year" do @durant.expects(:fetch_points_per_game).at_most_once ppg = @durant.points_per_game ppg2=…
Xbox ForSale
  • 11
  • 1
  • 2
1
vote
1 answer

Cannot load gem in IronRuby

I already removed all environment variables and ruby/ironruby directories and reinstalled it from scratch. And then I installed mocha through igem. Here are my outputs. $ ir IronRuby 0.9.1.0 on .NET 2.0.50727.3082 Copyright (c) Microsoft…
Marc Vitalis
  • 2,129
  • 4
  • 24
  • 36
1
vote
1 answer

How do I bypass a call to super method in test

I have a basic structure like this class Automobile def some_method # this code sets up structure for child classes... I want to test this end end class Car < Automobile def some_method super # code specific to Car... it's tested…
alkalinecoffee
  • 1,003
  • 8
  • 20
1
vote
1 answer

How to print to stdout from system command in Rake?

I have the following Rakefile desc "Runs tests" namespace :test do task :api do `mocha` end end When I run the command rake test:api, I don't get the nice output of dots that I would if I just ran the command mocha. How do I print…
Shane Stillwell
  • 3,198
  • 5
  • 31
  • 53
1
vote
3 answers

Mocha: Why can't I stub #caller method?

using Mocha in simple code turned in unexpected way. Could you explain what is going wrong? require 'test-unit' require 'mocha' class A def m caller.first end end So using this simple class, we can get the latest caller: A.new.m #=>…
Mark Huk
  • 2,379
  • 21
  • 28
1
vote
1 answer

Installing Mocha 0.9.7 in a Rails 2.3.3 project

I installed the Mocha 0.9.7 Rails plug-in using: $ script/plugin install git://github.com/floehopper/mocha.git (Just followed instruction in http://mocha.rubyforge.org/) Then, I have the following set-up defined in my functional test def setup …
Chry Cheng
  • 3,378
  • 5
  • 47
  • 79
0
votes
1 answer

undefined method `receive' in Rspec Rails

I am trying to test a worker in Rspec using receive. (This test in for my user.rb model) require 'rails_helper' require 'cancan/matchers' describe User, type: :model do context 'when an user' do subject(:ability) { Ability.new(user) } …
theKid
  • 522
  • 6
  • 19
0
votes
1 answer

Stubbing controller methods in Rails

I have some code like this: new_method = lambda do puts "Hey, I'm the new method!" redirect_to login_path end MyController.any_instance.stubs(:my_method).returns(new_method) The problem is it's returning the lambda, instead of calling it... So…
Mirror318
  • 11,875
  • 14
  • 64
  • 106
0
votes
1 answer

Global mock with mocha

I have many tests for my class. When I added check for file existence, in my class. I needed to add this code in all my cases. File.any_instance. expects(:exist?). with('test_file'). returns(true). once() But I want declare a global…
Rusty Robot
  • 1,305
  • 1
  • 12
  • 18
0
votes
1 answer

Using instance_of parameter matcher in mocha

I want to use a instance_of parameter matcher for a test in ActionController::TestCase. I have imported the following files require 'mocha/api' require 'mocha/setup' I wish to write something like…
thebenman
  • 1,621
  • 14
  • 35
0
votes
1 answer

Minitest/Mocha: Testing that a value changes multiple times

Let's say I have a service class as such: class FooService def self.execute(foo_id) Foo.find(foo_id).tap do |foo| foo.update_attribute :status, :working do_work(foo) foo.update_attribute :status, :done end end end A…
user1032752
  • 751
  • 1
  • 11
  • 28