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
4
votes
2 answers

How to stub Swift "Trait/Mixin" method in struct/class for testing

I have recently read about how to add "Traits/Mixins" to a struct/class in Swift by creating a protocol and extending that protocol with a default implementation. This is great as it allows me to add functionality to view the controller without…
5StringRyan
  • 3,604
  • 5
  • 46
  • 69
4
votes
1 answer

Is grpc channel/stub thread safe in csharp

is grpc channel thread safe in csharp, or more generally in any language depend on C core version; for the following code: 1) is channel thread safe? 2) is client thread safe? Channel channel = new Channel("127.0.0.1:50051",…
lcm
  • 531
  • 1
  • 5
  • 6
4
votes
0 answers

JS: Stub a method to do unit test via testdouble

I'm trying to 'stub' a method via testdoubleJS to do a unit test for this method (doing npm test). It is the first time I'm doing this, so it is still hard to understand for me. For my attempt - shown below - I do get the error TypeError:…
user3142695
  • 15,844
  • 47
  • 176
  • 332
4
votes
2 answers

Can OCMock mock a class such that it automatically uses the mocked instance in the code under test without injecting it

I'm new to OCMock and had a question. Can we stub a method of a class, where calling that method over any instance/object of that class gets mocked For Example : if Class_A has a non static function_a and Class_B has function_b which internally…
4
votes
1 answer

rspec stub any_instance with attributes

I need to stub all instances of a model that have a particular attribute or set of attributes. For example, using ActiveRecord: let(:model1) { Model.create!(uid: 1) } let(:model2) { Model.create!(uid: 2) } before do allow(model1).to…
stevenspiel
  • 5,775
  • 13
  • 60
  • 89
4
votes
1 answer

Rspec prevent method from being called

I am testing a controller method for creating new orders (e-commerce-like app). If user is present in the system, he should be redirected to new_user_session_path, else to new_order_path. Simple as that. This is my orders_controller.rb def new …
mohnstrudel
  • 639
  • 8
  • 22
4
votes
1 answer

node express es6 sinon stubbing middleware not working

I am writing the mocha unit test for the my express router. I found that however I try to stub the middleware, it still execute the middleware code. Here is my router & test, could anyone figure out? Router: import { aMiddleware, bMiddleware,…
benson
  • 41
  • 3
4
votes
1 answer

Subclassing RxSwift/Reactive extension for stubbing

Say I have a Field class with a harvest function like this: class Field { func harvest(handler: (Vegetable) -> Void) { … handler(carrot) … handler(potato) … handler(carrot) … } } I…
Luis Valdés
  • 1,409
  • 1
  • 12
  • 12
4
votes
4 answers

Implementing a protected parameterless constructor for unit testing

If I have a type with a big-old (lots of params) constructor, is it a valid approach to implement a protected parameterless constructor simply for the purposes of creating a derived "Fake" type to use for stubbing in unit tests? The alternative is…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
4
votes
1 answer

how do I use sinon sandboxes when tests run asynchronously?

I've got some code I'm trying to test with a structure like this (per Cleaning up sinon stubs easily): function test1() { // manually create and restore the sandbox var sandbox; beforeEach(function () { sandbox =…
Dov Rosenberg
  • 673
  • 6
  • 20
4
votes
1 answer

Using Ruby's Struct to stub an object for minitest

This is working but it feels sloppy to me. I'm wondering if it is a code smell or if there is a better way to be accomplishing this result. Basic question is how to stub some arbitrary object in ruby. I'm testing an edge case- that the final value…
erikdstock
  • 1,117
  • 9
  • 16
4
votes
3 answers

Software engineering with Ada: stubs; separate and compilation units

I'm with a mechanical engineering background but I'm interested to learn good software engineering practice with Ada. I have a few queries. Q1. If I understand correctly then someone can just write a package specification (ads) file, compile it and…
yCalleecharan
  • 4,656
  • 11
  • 56
  • 86
4
votes
1 answer

Why is my stubbed method returning null?

I have a class Dummy. I inject 3 variables. However, one of them is not injectable because it is an interface. So I inject an object, one of whose methods return the needed type. Class Dummy { private final Class1 class1; private final Class2…
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
4
votes
1 answer

How can I stub rand in minitest?

I've tried Random.stub :rand, 1 do ... end and Kernel.stub :rand, 1 do ... end and Class.stub :rand, 1 do ... end (because when I run self.class where I run rand(2) I get Class). I've also tried replacing rand(2) with Random.rand(2) but it doesn't…
Camden Narzt
  • 2,271
  • 1
  • 23
  • 42
4
votes
2 answers

PHPUnit stubbing: default return value from map

I have read in the PHPUnit Manual that with the following example, the method call doSomething('a','b','c') will return d and the method call doSomething('e','f','g') will return h.
physicalattraction
  • 6,485
  • 10
  • 63
  • 122