Questions tagged [stubbing]

Stubbing is modifying an instance method or property of an object at runtime to extend its functionality.

420 questions
179
votes
33 answers

Mockito - NullpointerException when stubbing Method

So I started writing tests for our Java-Spring-project. What I use is JUnit and Mockito. It's said, that when I use the when()...thenReturn() option I can mock services, without simulating them or so. So what I want to do is, to…
user5417542
  • 3,146
  • 6
  • 29
  • 50
169
votes
9 answers

Cleaning up sinon stubs easily

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' …
austinbv
  • 9,297
  • 6
  • 50
  • 82
89
votes
5 answers

Stubbing authentication in request spec

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__) +…
Jonas Bylov
  • 1,494
  • 2
  • 15
  • 28
85
votes
7 answers

RSpec: how to test Rails logger message expectations?

I am trying to test that the Rails logger receives messages in some of my specs. I am using the Logging gem. Let's say that I have a class like this: class BaseWorker def execute logger.info 'Starting the worker...' end end And a spec…
keruilin
  • 16,782
  • 34
  • 108
  • 175
51
votes
8 answers

How to stub a Typescript-Interface / Type-definition?

I work with Typescript on an AngularJS 1.X project. I use different Javascript libraries for different purposes. To unit-test my source I would like to stub some dependencies using the Typings (= interfaces). I don't want to use the ANY-type and…
user1879408
  • 1,948
  • 3
  • 17
  • 27
45
votes
1 answer

Is there a way to stub a method of an included module with Rspec?

I have a module that is included in another module, and they both implement the same method. I would like to stub the method of the included module, something like this: module M def foo :M end end module A class << self include M …
user3775153
  • 451
  • 1
  • 4
  • 5
38
votes
3 answers

RhinoMocks - Stub a Method That Returns a Parameter

I am using RhinoMocks, I need to stub a method, and always have it return the third parameter, regardless of what is passed in: _service.Stub(x => x.Method(parm1, parm2, parm3)).Return(parm3); Obviously, it ain't that easy. I don't always know…
Martin
  • 11,031
  • 8
  • 50
  • 77
32
votes
3 answers

Stubbing window.location.href with Sinon

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', () => { …
Francesco Pezzella
  • 1,755
  • 2
  • 15
  • 18
29
votes
3 answers

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

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…
Michael Bavin
  • 3,944
  • 6
  • 31
  • 35
29
votes
6 answers

How should I stub a method globally using RSpec?

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 …
p.matsinopoulos
  • 7,655
  • 6
  • 44
  • 92
26
votes
1 answer

How to verify if a specific function is called

I'm trying my hand at writing TDD in Go. I am however stuck at the following. The test to write: func TestFeatureStart(t *testing.T) {} Implementation to test: func (f *Feature) Start() error { cmd := exec.Command(f.Cmd) cmd.Start() } How…
Patrick
  • 489
  • 2
  • 8
  • 12
26
votes
2 answers

Rspec 3.0 How to mock a method replacing the parameter but with no return value?

I've searched a lot and just cannot figure this out although it seems basic. Here's a way simplified example of what I want to do. Create a simple method that does something but doesn't return anything, such as: class Test def test_method(param) …
user3060126
  • 461
  • 1
  • 4
  • 13
25
votes
4 answers

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

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 …
Zequez
  • 3,399
  • 2
  • 31
  • 42
25
votes
3 answers

RSpec Stubbing: Return the parameter

Though my question is pretty straightforward, I failed to find an answer around here: How can I stub a method and return the parameter itself (for example on a method that does an array-operation)? Something like this: …
Len
  • 2,093
  • 4
  • 34
  • 51
22
votes
5 answers

Rhino Mocks stubs and mocks are only good for interfaces?

Is it correct that Rhino Mocks stubs and mocks are only good for interfaces, not concrete classes? I spent quite a time trying to make this piece of code working. I did not expect the stubbed pubSubClient to always call Send method from the class.…
Dmitry Duginov
  • 547
  • 2
  • 4
  • 15
1
2 3
27 28