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

How to mock ActsAsTaggableOn with RSpec and Mocha?

How can i mock ActsAsTaggableOn::tag using RSpec and Mocha ? I'm new to rails testing so i might be missing something obvious... I tried : /spec/controllers/books_controller.rb it "should return 2 categories whith books" do tag_ruby =…
vdaubry
  • 11,369
  • 7
  • 54
  • 76
2
votes
2 answers

How do you mock a method like "each"?

I'm trying to test a method that uses CSV.foreach to read in a csv file and does some processing on it. It looks something like this: require 'csv' class Csv_processor def self.process_csv(file) begin CSV.foreach(file) do { |row| …
Mike E
  • 5,493
  • 1
  • 14
  • 15
2
votes
1 answer

Can't stub with mocha in ruby on rails test

I'm fairly new to Ruby/Ruby on Rails and having trouble stubbing out a method via mocha in an existing codebase. I've simplified the code down to a MWE where this breaks. Here is test_helper.rb: ENV['RAILS_ENV'] ||= 'test' require…
LateCoder
  • 2,163
  • 4
  • 25
  • 44
2
votes
1 answer

Mocking an external API

I'm new to testing strategies and mocking, and I'm having a tough time figuring out how to mock a call to an external service. I'm sure it's something easy I'm missing, I just don't know what exactly. I'm using the Braintree gem to charge for…
joeellis
  • 2,745
  • 7
  • 28
  • 45
2
votes
2 answers

Stubbing write method inside File.open using Mocha in Rails

My spec_helper.rb has the following configuration: RSpec.configure do |config| config.mock_with :mocha # .. other configs end I want to test the following piece of code: File.open('filename.zip', 'wb') do |file| file.write('some file…
arunvelsriram
  • 1,036
  • 8
  • 18
2
votes
0 answers

Mocha. expects without stubbing

I there something like this for Mocha. I have this: reservation.expects(:publish_to_sns).twice reservation.save reservation.update_attribute :first => 'qweqwe' And I would like publish_to_sns to perform its function.
sites
  • 21,417
  • 17
  • 87
  • 146
2
votes
1 answer

Ruby Mocha - how to specify ".with" multiple ".times"

I would like to be able to chain multiple .with expectations together. At present I am putting them on separate lines - foo.expects( :bar ).with( a ) foo.expects( :bar ).with( b ) foo.expects( :bar ).with( c ) With .returns you can just do…
Bungus
  • 592
  • 2
  • 11
2
votes
3 answers

How to stub method on instance variable?

So I have this simple ruby class: class GetRequestList def initialize(current_user, filter_hash) @authorizer = RequestAuthorizer.new(current_user) @filter = RequestFilter.new(filter_hash) end def generate …
lompy
  • 341
  • 1
  • 3
  • 12
2
votes
1 answer

Testing rack-timeout in sinatra and ruby

This is something that I thought would be straightforward but I'm having issues around testing the rack-timeout gem. I have a sinatra base class with an endpoint which does some logic. module MyModule class MySinatra < Sinatra::Base use…
Rooktone
  • 109
  • 1
  • 5
2
votes
2 answers

mocha expectation error when trying to bring up rails server

I had a perfectly running rails project, but along the lines i had to reinstall a couple of gems and switched between rbenv and rvm. I was previously running rvm, then i switched to rbenv, started getting a bunch of exceptions, tried switching back…
KG -
  • 7,130
  • 12
  • 56
  • 72
2
votes
2 answers

cannot load such file -- mocha/object (LoadError)

When I run rspec to test, I got this error: cannot load such file -- mocha/object (LoadError) The problem is with the line: config.mock_with :mocha below Here is my spec_helper.rb: require 'rubygems' require 'spork' Spork.prefork do # Loading…
simo
  • 23,342
  • 38
  • 121
  • 218
2
votes
2 answers

Stubbing out class method using Mocha

I have a method I want to test using rspec and Mocha. My aim is to stub out Dir.glob in order to just return a simple array of one file. The method is defined as so: def my_method Dir.glob("#{upload_dir}/*.pdf").each do |file| ... …
jackbot
  • 2,931
  • 3
  • 27
  • 35
1
vote
2 answers

Assert method call without stub?

I would like to assert that a particular method was called, but I don't want to mock/stub the method - I want to include that code in the test as well. For example, in Ruby, something like: def bar() #stuff I want to test end def foo() if…
Daniel Tsadok
  • 573
  • 5
  • 10
1
vote
1 answer

How to use mocha outside of unit tests?

I'm trying to use mocha outside of unit tests to mock an Net::HTTPResponse object. here is a simple example: #!/usr/bin/env ruby -w require 'net/http' require 'rubygems' require 'mocha' response = mock('Net::HTTPResponse') response.stubs(:code =>…
Julien
  • 5,729
  • 4
  • 37
  • 60
1
vote
1 answer

Unable to create a mock object with mocha

I keep getting this error... mockresponse.rb:4:in `createResponseObject': undefined method `mock' for main:Object (NoMethodError) Here's my code (a code example I swiped): require 'mocha' def createResponseObject @http_mock =…
user479808
  • 643
  • 1
  • 5
  • 6