Mocha Mock Carries to Another Test

Mocha Mock Carries To Another Test

I had the same problem, mocked functionality was not isolated to a test, it seems to be a problem with the load order of Mocha.

I had some issues getting Mocha to work with Rails3. I found a few stackoverflow posts regarding, but didn't stumble across the solution until I found a post on agoragames.com

Basically, in the Gemfile of your project, the require for Mocha should look like:

gem 'mocha', :require => false

Then in test/test_helper.rb, add a require line for mocha:

...
...
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'mocha'

class ActiveSupport::TestCase
...
...

I think the require line for mocha in the Gemfile means that you need to already have mocha installed as a gem, bundler won't take care of it for you.

Mocha not mocking a class method in a functional test (Rails 3)

As said on Twitter:
You're setting you your mock after you're doing your request :-)

Testing if a function is called using Mocha

or just

object.expects(:function_name).twice

alternatively, if it has differnet input you should test that

resultmock = mock 
object.expects(:function_name).with(someobject).returns(mock)
resultmock.expects(:something).returns(true)
object.expects(:function_name).with(resultmock)

don't know if this helps, but it should give you a kick start. FYI: 'once' is default. Good luck, do TDD (=test-first) or mocking will be a pain :)

Be sure to load mocha last, so it is really being loaded, as in my answer here:
Mocha Mock Carries To Another Test

NoMethodError: undefined method `mock' with Mocha and Rails 3

I found the answer immediately after I'd posted this.

Here's the answer: Mocha Mock Carries To Another Test

In short, I changed

gem 'mocha', '0.10.0'

to

gem 'mocha', '0.10.0', :require => false

and it worked like a charm!

How to mock function for unit testing in serverless-mocha-plugin

I found a way to mock function in serverless-mocha-plugin for aws using nodejs

It can be done using sinonjs http://sinonjs.org/

Here is example for above function
In order to mock loginMethods

const loginPromise = new Promise(function (resolve, reject) {
const loginRes = {

"status": "success",
"hash": "U2_a5da71a9-4295-48e7-b427-843c17c8cae3",
"firstName": "Guest",
"lastName": "G",
};
resolve(loginRes);
});

var loginMock = sinon.mock(loginMethods);
loginMock.expects('login').withArgs(arg1, arg2).returns(loginPromise);

In this way while test when this function will be called it will only call mock function not the original function and response will also be mocked response

Mocha: Silence satisfied expectations

You could monkey patch mocha to achieve this. There's a method on Mocha::Mockery that returns the satisfied expectations which you could patch to return an empty array:

module Mocha
class Mockery
def satisfied_expectations
[]
end
end
end

If you put this in test_helper.rb it'll get picked up.

Alternatively for a bit more flexibility you could opt to only hide them when an environment variable is set:

module Mocha
class Mockery
def satisfied_expectations_with_optional
if ENV['MOCHA_HIDE_SATISFIED']
[]
else
satisfied_expectations_without_optional
end
end
alias_method_chain :satisfied_expectations, :optional
end
end

Then run your tests like this:

> MOCHA_HIDE_SATISFIED=1 rake test


Related Topics



Leave a reply



Submit