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
0 answers

Why doesn't Mocha complain about expectations?

I'm messing around with Mocha for days and cannot seem to get it working properly. I'm using Rails 3.1.0.rc4 and Mocha 0.9.12, running under Ruby 1.9.2-p180. In my Gemfile I have a gem 'mocha', :require => false and I require Mocha in the last line…
Michael Trojanek
  • 1,813
  • 17
  • 15
1
vote
2 answers

Returning mock objects from factory girl

I am using Mocha and Factory_girl in a JRuby rails application. When I call the factory I would like to return the objects with some mocking already done. Here is a code snippet of what I am trying to do. Factory.define :tweet_feed_with_tweets,…
Josh Moore
  • 13,338
  • 15
  • 58
  • 74
1
vote
1 answer

TDD with mocha and mongodb

I'm trying to add mongodb via Mongo Ruby Driver to my sinatra app, and I've decided to so it the right way. When I started to think about TDD I coudn't find any examples or guidance about how to do it with mongodb. After some time I started to think…
MexicanHacker
  • 2,685
  • 3
  • 19
  • 20
1
vote
1 answer

Unstubbing a class method in Mocha

For a particular test, I want to change the return value of a class method. I can get the correct behavior by calling MyClass.expects(:method).returns(:myvalue). How can I stop this behavior once I'm done with the test? There's an unstub method in…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
1
vote
1 answer

Ruby Mocha expect the first argument to be a symbol

I'm trying to assert a method call happens where the first argument is a symbol like so: Foo.bar(:some_key, {}) I don't really care what the second argument is at this stage. I've tried: Foo.expects(:bar).with(includes(:some_key)) and other…
Rimian
  • 36,864
  • 16
  • 117
  • 117
1
vote
2 answers

Ruby. Mocking in RSpec

I have a problem with mocking. I have class DistanceMatrix and I would like to indicate which method form_matrix was called in if/else statement. I need to use mocha and RSpec. Any ideas? class DistanceMatrix def initialize(*args) if…
Martynas
  • 2,545
  • 7
  • 30
  • 36
1
vote
1 answer

How to mock to twitter api using Ruby on Rails?

I'm developing a application that uses the twitter API. I'm currently using rspec with mocha, but I found it to be cumbersome and I cannot reuse the mocking that I create for a give method. Is there a way that you can have for a give call, return…
rafamvc
  • 8,227
  • 6
  • 31
  • 34
1
vote
1 answer

Testing two different expectations with mocking

I've recently just added Devise to my first Rails3 app, and I'm having a bit of trouble with the controller tests. I'm testing the User controller class, which is the same model that Devise uses. So at the beginning of my spec I have…
stuartc
  • 2,244
  • 2
  • 24
  • 31
1
vote
1 answer

How can I make Mocha test compile Ruby-ish Unicode string?

I'm invoking Telegram Bot API from a Node.js application. I need to send some Unicode characters to the API, that only accepts Ruby format (\u{1F680} outputs ). I store all Unicode characters in this format in a file that is required by the one…
iria
  • 11
  • 1
1
vote
0 answers

How to stub a method that is also the name of a hash in ruby?

I am new to ruby mocha and need to mock a method which is also a hash. Currently the method does two things: Products.elements.oldest # returns the oldest element Products.elements['second'] # returns the second element and what i did was…
Cloud_cal
  • 132
  • 2
  • 12
1
vote
1 answer

How do I verify the number of elements and content of an array using ParameterMatchers?

I am learning Ruby with TDD using Mocha and MiniTest. I have a class that has one public method and many private methods, so the only method my tests are going to tests are the public one. This public method does some processing and creates an array…
JSBach
  • 4,679
  • 8
  • 51
  • 98
1
vote
1 answer

stubs MailChimp api call for functional testing

I'm trying to test a MailChimp subscription to an specific list: test/functional/some_controller_test.rb require 'test_helper' class SomeControllerTest < ActionController::TestCase test "invalid signup" do …
nisevi
  • 627
  • 1
  • 10
  • 27
1
vote
1 answer

Mocking Sort With Mocha

How can I mock an array's sort expect a lambda expression? This is a trivial example of my problem: # initializing the data l = lambda { |a,b| a <=> b } array = [ 1, 2, 3, 4, 5 ] sorted_array = [ 2, 3, 8, 9, 1] # I expect that sort will be called…
user175750
  • 746
  • 6
  • 5
1
vote
1 answer

Mocha on Ruby: Check a stubbed function called once

It should be straight forward, but it doesn't work for me. I'm stubbing a function call, and I want to make sure it is called once, so I did: MyClass.stubs(:record).returns(true).expect(:record).once MyClass.run but I keep getting: expected…
Yossale
  • 14,165
  • 22
  • 82
  • 109