stubbing

Stub out address geocoding during RSpec unit test

有些话、适合烂在心里 提交于 2019-12-22 03:55:28
问题 I'm using the geocoder gem to add geocoding functionality to one of my Active Record model classes. This works great, but I don't actually want the geocoding to fire during unit tests. I've tried stubbing out the call to geocode by adding this to my RSpec test: before(:each) do User.stub!(:geocode).and_return([1,1]) end However, when I run my tests it still appears to be calling out to geocode. What am I doing wrong? FYI, this all works if I stub on the instance level (e.g. some_user.stub!

Overriding functions in other modules in node.js

被刻印的时光 ゝ 提交于 2019-12-21 20:47:54
问题 I'm trying to stub a function with nodeunit in a Node.js app. Here's a simplified version of what I'm trying to do: In lib/file.js : var request = require('request'); var myFunc = function(input, callback){ request(input, function(err, body){ callback(body); }); }; In test/test.file.js : var file = require('../lib/file'); exports['test myFunc'] = function (test) { request = function(options, callback){ callback('testbody'); }; file.myFunc('something', function(err, body){ test.equal(body,

stub method only on the first call with Rspec

感情迁移 提交于 2019-12-21 03:19:41
问题 How can I stub a method only on the first call, and in the second one it should behave as expected? I have the following method: def method do_stuff rescue => MyException sleep rand retry end I want to the first call of do_stuff to raise MyException , but in the second call, behaves normally. I need to achieve this to test my rescue block without getting an infinite loop. Is there a way to achieve this? 回答1: You can pass a block to a stub that will be invoked when the stub is called. You can

Stubbing Warden on Controller Tests

删除回忆录丶 提交于 2019-12-19 08:33:07
问题 I'm having an issue with testing my controllers and using Warden. All examples point at stubbing request.env['warden'] . This causes issues in my controllers when I call env['warden'] , which then returns nil . For a crude example, using this: request.env['warden'] = double(Warden, :authenticate => nil, :authenticate! => nil, :authenticated? => false) And a simple before filter like this: before_filter do redirect_to new_user_session_url unless env['warden'].authenticated? end I get a nil . I

Stubbing Warden on Controller Tests

自闭症网瘾萝莉.ら 提交于 2019-12-19 08:32:14
问题 I'm having an issue with testing my controllers and using Warden. All examples point at stubbing request.env['warden'] . This causes issues in my controllers when I call env['warden'] , which then returns nil . For a crude example, using this: request.env['warden'] = double(Warden, :authenticate => nil, :authenticate! => nil, :authenticated? => false) And a simple before filter like this: before_filter do redirect_to new_user_session_url unless env['warden'].authenticated? end I get a nil . I

Cleaning up sinon stubs easily

我怕爱的太早我们不能终老 提交于 2019-12-17 17:27:16
问题 Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this beforeEach -> sinon.stub some, 'method' sinon.stub some, 'mother' afterEach -> # I want to avoid these lines some.method.restore() some.other.restore() it 'should call a some method and not other', -> some.method() assert.called some.method 回答1: Sinon provides this functionality through the use of

Stubbing authentication in request spec

烂漫一生 提交于 2019-12-17 04:17:05
问题 When writing a request spec, how do you set sessions and/or stub controller methods? I'm trying to stub out authentication in my integration tests - rspec/requests Here's an example of a test require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/authentication_helpers' describe "Messages" do include AuthenticationHelpers describe "GET admin/messages" do before(:each) do @current_user = Factory :super_admin login(@current_user) end it "displays received messages

How to stub out a generic method definition in an interface using Microsoft Fakes in c#

北城余情 提交于 2019-12-14 02:36:27
问题 I have a unit test which stubs out the following interface using Microsoft Fakes: public interface ITable { Task<TableResult> Retrieve(string tableReference, string partitionKey, string rowKey); } The stub looks like this: ITable table = new MessagesAPI.Azure.Fakes.StubITable() { RetrieveStringStringString = delegate { TableResult tableResult = new TableResult(); return Task.FromResult(tableResult); } }; This works fine. However I'd like to change the interface to be more generic like so:

Play 2.0: FakeApplication calling a stub controller instead real one

不羁岁月 提交于 2019-12-13 02:13:15
问题 This is a continuation of a prior question in which answer I saw that I had a misconception about the use of plugins in FakeApplication. First I have a Model created as a trait and a object that implment it. trait UserModel extends ModelCompanion[User, ObjectId] { // ... } object User extends UserModel Next, I have a Controller, created as a abstract class that receive a instance of UserModel, and its respective implementation that uses the User object. abstract class UsersController extends

How to stub a method that is called from an outer scope to the function under test?

谁都会走 提交于 2019-12-12 11:29:41
问题 I have a Redis client that is created thus using the node_redis library (https://github.com/NodeRedis/node_redis): var client = require('redis').createClient(6379, 'localhost'); I have a method I want to test whose purpose is to set and publish a value to Redis, so I want to test to ensure the set and publish methods are called or not called according to my expectations. The tricky thing is I want this test to work without needing to fire up an instance of a Redis server, so I can't just