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

Stubbing Chained Queries in Rails 3 and Rspec

I'm trying to test a scope I have that is based upon a chain of other scopes. ("public_stream" below). scope :public, where("entries.privacy = 'public'") scope :completed, where("entries.observation <> '' AND entries.application <> ''") scope…
stillmotion
  • 4,608
  • 4
  • 30
  • 36
14
votes
2 answers

How to stub Python methods without Mock

I'm a C# dev moving into some Python stuff, so I don't know what I'm doing just yet. I've read that you don't really need Dependency Injection with Python. I've been told you can instantiate objects in your code and have them run the way you want,…
Corey Coogan
  • 1,569
  • 2
  • 17
  • 31
14
votes
3 answers

What is the difference between mocks, stubs and fake objects

Although there are plenty of resources, even here on SO, only two of the terms are compared to each other in these Q/A. So, in short, what is each one of them? And how they all relate to each other? Or don't they at all?
Marcel
  • 15,039
  • 20
  • 92
  • 150
14
votes
3 answers

When to Expect and When to Stub?

I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect: this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call…
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
13
votes
4 answers

java.net.ConnectException :connection timed out: connect?

I have used RMI in my code : import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1,double d2) throws RemoteException; } import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends…
saplingPro
  • 20,769
  • 53
  • 137
  • 195
13
votes
5 answers

In Cypress, how do I stub a POST API request with parameters in the body?

I am writing an end-to-end test with Cypress and I would like to stub the network requests which my application makes. Specifically, I would like to stub out multiple POST requests which have parameters in the body and to change my simulated…
Andrew Farrell
  • 2,368
  • 2
  • 22
  • 25
13
votes
2 answers

laravel create model from custom stub when using php artisan

When I use php artisan make:model CustomNamespace\TestModel, I get a model based on default stub as this : namespace App\Models\CustomNamespace; use Illuminate\Database\Eloquent\Model; class TestModel extends Model { // } But what I want to…
4givN
  • 2,936
  • 2
  • 22
  • 51
13
votes
3 answers

Codeception\Util\Stub methods ::exactly and ::once don't work

I am using Codeception\Util\Stub to create unit tests. And I want to be sure that my method called several times. For this I am using method 'exactly'. Example: use \UnitTester; use \Codeception\Util\Stub as StubUtil; class someCest { public…
vatvit
  • 131
  • 1
  • 5
13
votes
3 answers

Prevent stubbing of equals method

I would like to test my class' equals() method but Mockito seems to be calling the stub version every time. My test is as follows; PluginResourceAdapter adapter = mock (PluginResourceAdapter.class); PluginResourceAdapter other = mock…
SJunejo
  • 1,316
  • 4
  • 23
  • 39
13
votes
5 answers

Stub one method of class and let other real methods use this stubbed one

I have a TimeMachine class which provides me current date/time values. The class looks like this: public class TimeMachine { public virtual DateTime GetCurrentDateTime(){ return DateTime.Now; }; public virtual DateTime GetCurrentDate(){…
Buthrakaur
  • 1,821
  • 3
  • 23
  • 35
12
votes
4 answers

PHPUnit: Stubbing multiple interfaces

I'm getting to grips with PHPUnit, and have so far found it pretty easy to use, but I've run up against a test case that's causing me difficulty. I'm writing code against a set of interfaces that objects are expected to implement (some PHP ones,…
GordonM
  • 31,179
  • 15
  • 87
  • 129
12
votes
6 answers

Stubbing out functions or classes

Can you explain the concept stubbing out functions or classes taken from this article? class Loaf: pass This class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, so you use pass.…
xralf
  • 3,312
  • 45
  • 129
  • 200
12
votes
1 answer

PyCharm not inserting docstring stub for class?

I have the following piece of code: class Note: def __init__(self, note=None, duration=None, start_time=None): self.note = note self.duration = duration self.start_time = start_time I wanted to generate a docstring for…
pyjamas
  • 4,608
  • 5
  • 38
  • 70
12
votes
1 answer

Expect the creation of a new object

I need to write an expectation that a new object will be created in a payment system. Code included from order.rb and order_spec.rb (Order class is here): #order.rb def confirm_order(method_of_payment) if…
Ben Hawker
  • 949
  • 8
  • 15
12
votes
1 answer

how to stub HttpControllerContext

I am trying to unit-test a piece of code that gets called from a WebAPI (OData) controller and takes in an HttpControllerContext: public string MethodToTest(HttpControllerContext context) { string pub = string.Empty; if (context != null) …
stefjnl
  • 732
  • 3
  • 14
  • 31