stubbing

Overriding functions in other modules in node.js

时间秒杀一切 提交于 2019-12-04 12:44:16
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, 'testbody'); test.done(); }); }; It seems like I'm not overriding request properly, because when I try to

Testing class method using OCMock release 2.1.1

☆樱花仙子☆ 提交于 2019-12-04 06:49:46
问题 I am trying to check if a class method is getting invoked using OCMock. I have gathered from OCMock website and other answers on SO that the new OCMock release (2.1) adds support for stubbing class methods. I am trying to do the same: DetailViewController: +(BOOL)getBoolVal { return YES; } Test Case: -(void) testClassMethod { id detailMock = [OCMockObject mockForClass:[DetailViewController class]]; [[[detailMock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] getBoolVal:nil]; } The test is

Should I stub the model in Factory girl or in the spec file while testing?

为君一笑 提交于 2019-12-03 23:46:43
Almost every spec file I come accross I end up writing stuff like: before :each do @cimg = Factory.build :cimg_valid @cimg.stub(:validate_img).and_return true @cimg.stub(:validate_img_url).and_return true @cimg.stub(:save_images).and_return true @cimg.stub(:process_image).and_return true @cimg.stub(:img).and_return true end I mean, the model I get from Factory.build is completely valid. But if I don't stub that stuff it saves things in the filesystem, and validates stuff I'm not testing... What I mean, I think it would be cleaner to do something like this: before :each do @cimg = Factory.build

stub_chain together with should_receive

浪子不回头ぞ 提交于 2019-12-03 16:31:41
问题 I am trying to test if in a method calling chain one of the methods get a specific parameter. In the below code for example MyModel must receive the parameter 0 for the method offset . Unfortunately the code below does not work. It seems it is not possible to mix should_receive and stub_chain. How could I solve this? I am using RSpec 2. MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts, :offset, :limit, :order).and_return([]) # does not work! The code I am trying to test: tags =

How should I stub a method globally using RSpec?

前提是你 提交于 2019-12-03 14:38:36
问题 I am working on a Rails application. I am trying to stub a method globally. What I am doing is to stub it inside the RSpec configuration, on a before(:suite) block as follows: RSpec.configure do |config| config.before(:suite) do allow_any_instance_of(MyModel).to receive(:my_method).and_return(false) end end However, starting the test fails with the following error: in `method_missing': undefined method `allow_any_instance_of' for #<RSpec::Core::ExampleGroup:0x00000008d6be08> (NoMethodError)

stub method only on the first call with Rspec

你说的曾经没有我的故事 提交于 2019-12-03 09:56:46
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? You can pass a block to a stub that will be invoked when the stub is called. You can then perform the unstub in there, in addition to doing whatever you need to. class Foo def initialize @calls

Mockito: How to easily stub a method without mocking all parameters

风格不统一 提交于 2019-12-03 09:46:15
I have a method i'd like to stub but it has a lot of parameters. How can i avoid mocking all parameters but still stub the method. Ex: //Method to stub public void myMethod(Bar bar, Foo foo, FooBar fooBar, BarFoo barFoo, .....endless list of parameters..); I don't quite follow what problem you're having using Mockito. Assuming you create a mock of the interface that contains your myMethod() method, you can then verify only the parameters to the method that you are interested in. For example (assuming the interface is called MyInterface and using JUnit 4): @Test public void test() { MyInterface

How should I stub a method globally using RSpec?

家住魔仙堡 提交于 2019-12-03 04:27:12
I am working on a Rails application. I am trying to stub a method globally. What I am doing is to stub it inside the RSpec configuration, on a before(:suite) block as follows: RSpec.configure do |config| config.before(:suite) do allow_any_instance_of(MyModel).to receive(:my_method).and_return(false) end end However, starting the test fails with the following error: in `method_missing': undefined method `allow_any_instance_of' for #<RSpec::Core::ExampleGroup:0x00000008d6be08> (NoMethodError) Any clue? How should I stub a method globally using RSpec? P. It probably is a context / initialization

Stubbing Warden on Controller Tests

倖福魔咒の 提交于 2019-12-01 06:48:42
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 just managed to get it working using controller.env['warden'] = ... and it works. This makes sense,

Stubbing window.location.href with Sinon

大憨熊 提交于 2019-12-01 02:12:00
I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon. What I have tried so far ( using this example ): describe('Logger', () => { it('should compose a Log', () => { var stub = sinon.stub(window.location, 'href', 'http://www.foo.com'); }); }); The runner displays the following error: TypeError: Custom stub should be a function or a property descriptor Changing the test code to: describe('Logger', () => { it('should compose a Log', () => { var stub = sinon.stub(window.location, 'href', { value: 'foo' }); }); }); Which