Questions tagged [stub]

A replacement implementation for objects, methods, or functions in order to remove external dependencies, or the object at the client end of an RPC or RMI invocation.

A replacement implementation for object, method, or function. The typical types of stubs are:

  • Testing stubs that remove external dependencies. They are typically used during unit and component testing. If you're trying to write a unit test and need to replace a simple call to a database, external libraries (e.g., file I/O) or other system API, stubbing might be perfectly suited for your needs.
  • RPC (CORBA, RMI, web service) stubs forward the call to remote service where it is handled; the answer, if any, is returned to the calling side. They implement the same interface that is implemented by the servicing object on remote side (may also implement additional interfaces).
  • Dummy stubs are empty placeholders for objects, methods or functions that are supposed to be completed later. They allow the current code to compile so that other, already finished parts could be tested. Such stubs are used when implementing a very large libraries following some already specified API.

A mock is somewhat similar to the testing stub but is not considered stub as it is a special purpose object capable of registering that has been called on it. It does not take other actions (a generally unfavoured partial mock does forwards execution of some methods to underlying object).

1108 questions
9
votes
1 answer

How can I get my Angular5 spyOn to work as expected?

My test is not detecting changes. Here is my component: toggleUploadModal() { const modalRef = this.ngbModal.open(UploadFilesComponent, { size: 'lg', backdrop: 'static' }); modalRef.componentInstance.DeliverableTransaction =…
Shamoon
  • 41,293
  • 91
  • 306
  • 570
9
votes
2 answers

Stub calls to third party services using WireMock

I tried to find a way to stub calls to external services via WireMock. WireMock easily mocks any relative URL, but what if we want to intercept a REST call, which was sent from our node to some 3rd party service and return predefined response? Is…
Alesto
  • 639
  • 4
  • 13
  • 29
9
votes
3 answers

PHPUnit - multiple stubs of same class

I'm building unit tests for class Foo, and I'm fairly new to unit testing. A key component of my class is an instance of BarCollection which contains a number of Bar objects. One method in Foo iterates through the collection and calls a couple…
keithjgrant
  • 12,421
  • 6
  • 54
  • 88
9
votes
1 answer

How to mock angularjs $window.sessionStorage with Jasmine and Karma

I'm brand new to Jasmine and trying to figure out how to mock the $window.sessionStorage object when testing an angularjs SPA using Jasmine. (function() { 'use strict'; var serviceId =…
veilig
  • 5,085
  • 10
  • 48
  • 86
9
votes
2 answers

Unable to stub redis in rspec tests

I'm trying to stub redis using the mock_redis gem for my rspec tests. My rspec config looks like this: RSpec.configure do |config| # ... various rspec config options ... config.before(:each) do redis_instance = MockRedis.new …
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
9
votes
1 answer

With Rhino Mocks how to stub a method that makes use of the params keyword?

I am attempting to setup an expectation on a repository. The method makes use of the params keyword: string GetById(int key, params string[] args); The expectation I have setup: var resourceRepo =…
ahsteele
  • 26,243
  • 28
  • 134
  • 248
9
votes
2 answers

How write stub method with NUnit in C#

I have 2 classes: FirstDeep.cs SecondDeep.cs I did simple code for example: class FirstDeep { public FirstDeep() { } public string AddA(string str) { SecondDeep sd = new SecondDeep(); bool…
Smit
  • 609
  • 3
  • 11
  • 27
8
votes
6 answers

How to stub require() / expect calls to the "root" function of a module?

Consider the following jasmine spec: describe("something.act()", function() { it("calls some function of my module", function() { var mod = require('my_module'); spyOn(mod, "someFunction"); something.act(); …
jbpros
  • 1,637
  • 1
  • 15
  • 17
8
votes
1 answer

Performance testing with external dependencies

When performance testing in the microservices world (talking mainly load testing), what is your approach regarding external dependencies (APIs) your application relies on, but not owned/controlled by your team. In my case the external dependencies…
martbon
  • 81
  • 3
8
votes
3 answers

When using SinonJS in Typescript private properties are reported as missing

I'm trying to stub a class instance using sinon.createStubInstance but I'm getting an error stating that a private member variable is missing. Of course I can't explicitly set it either because it's a private member. Example classes: class Foo { …
Mark
  • 103
  • 6
8
votes
3 answers

Rhino Mocks problems with private setter in stub

Error: You are trying to set an expectation on a property that was defined to use PropertyBehavior. Instead of writing code such as this: mockObject.Stub(x => x.SomeProperty).Return(42); You can use the property directly to achieve the…
Daniel
  • 8,133
  • 5
  • 36
  • 51
8
votes
1 answer

httptest.NewRequest set Context stub

I am creating a request stub in order to pass it to function under the tested: request := httptest.NewRequest("GET", "http://example.com/foo", nil) Question: can I also stub Context object for this request by adding request-uuid Value to it?
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
8
votes
5 answers

Testing: mocking node-fetch dependency that it is used in a class method

I have the following situation: A.js import fetch from 'node-fetch' import httpClient from './myClient/httpClient' export default class{ async init(){ const response = await fetch('some_url') return…
8
votes
1 answer

Stubbing an ES6 class method using Mocha and Sinon in NodeJS

Is there a way to stub an ES6 class method using Mocha/Sinon? I'm trying to do this... sinon.stub(Factory, 'announce'); but I just get the following error... TypeError: Attempted to wrap undefined property announce as function
michael
  • 4,427
  • 6
  • 38
  • 57
8
votes
3 answers

ContentResolver.requestSync in Sync Adapter is not working in Android

I am trying to write a sync adapter with 'StubProvider' and 'StubAuthenticator', i followed the offical guidelines, my code is running without any errors but 'onPerformSync()' is NOT getting called, i tried everything but no use. My full project can…
Kazmi
  • 593
  • 1
  • 6
  • 24