Questions tagged [stub]

17 questions
120
votes
8 answers

How exactly should unit tests be written without mocking extensively?

As I understand, the point of unit tests is to test units of code in isolation. This means, that: They should not break by any unrelated code change elsewhere in the codebase. Only one unit test should break by a bug in the tested unit, as opposed…
Alex Lomia
  • 1,301
  • 2
  • 9
  • 9
35
votes
3 answers

What does stubbing mean in programming?

I often hear the term "stub", "stub something out", "stubs", and so forth. What does stubbing mean in programming, and where does the word come from? In what contexts can it be used?
22
votes
3 answers

Is it okay to fake part of the class under test?

Suppose I have a class (forgive the contrived example and the bad design of it): class MyProfit { public decimal GetNewYorkRevenue(); public decimal GetNewYorkExpenses(); public decimal GetNewYorkProfit(); public decimal GetMiamiRevenue(); …
User
  • 1,541
  • 2
  • 16
  • 30
14
votes
4 answers

Do mocks violate the Open/Closed principle?

Some time ago I read, on a Stack Overflow answer that I can't find, a sentence that explained that you should test public APIs, and the author said that you should test interfaces. The author also explained that if a method implementation changed,…
Christopher Francisco
  • 2,392
  • 1
  • 12
  • 27
4
votes
3 answers

Should I make my class mockable by marking its methods as virtual or by creating an interface?

In the process of refactoring non-testable code we are re-designing some of ours classes so that it can be easily replaced by a stub or mock during unit tests. Here is an example of such class (implementation is omitted for simplification): class…
Delgan
  • 366
  • 2
  • 13
3
votes
2 answers

Has anyone used a Stub in production code?

I have a situation where some procedure returns an object (kind of like a DTO) with a certain interface: interface ISomeInterface { string StringReadOnlyProperty { get; } int IntReadWriteProperty { get; set; } } (In the real implementation,…
Scott Whitlock
  • 21,874
  • 5
  • 60
  • 88
3
votes
1 answer

Stubbing and mocking boundaries

Suppose I'm building a JSON API and would like to include an endpoint that returns a list of recently created posts. Somewhere in my system is an existing Post model with the following test: create a few posts in the db (some new, some old) posts…
scttnlsn
  • 179
  • 1
  • 3
2
votes
3 answers

How can I avoid chasing my own tail when testing against complicated return values?

Sometimes there are functions that return complicated data and cannot be divided any further e.g. in the area of signal processing or when reading and decoding a bytestring to another format. How am I supposed to create stubs (e.g. the bytestring)…
Agent49
  • 23
  • 4
2
votes
1 answer

Is the distinction between Mocks, Stubs and Fakes useful?

Automated software testing professionals often make a distinction between various kinds of test doubles such as Mocks, Stubs and Shim/Fakes. In fact, in the article Mocks Aren't Stubs Martin Fowler describes clearly the difference between the types…
Chedy2149
  • 547
  • 1
  • 5
  • 9
2
votes
1 answer

Keeping Stubs in Sync?

If I want to test frontend code (e.g. react SPA) that queries the backend and I stub out the responses from the backend using sinon fake server/fake XHR, then what happens if the backend code changes? If the backend changes then the frontend tests…
2
votes
4 answers

Test Doubles, Mocks and Stubs - when not to use & why, for Ruby, Rspec

I've learned about mocking and stubbing and I've seen how they can help me create great test suites that run blindingly fast and thus speed up my development process hugely. However I've also seen the downside when there is an issue that is database…
Michael Durrant
  • 13,101
  • 5
  • 34
  • 60
1
vote
0 answers

Dealing with stubbed external dependency

In sut I should I have two kinds of dependencies to be stubbed: 3rd party dependencies (mongodb & co) and own dependencies: var async = require('async'), // This dependency should not be stubbed mongodb = require('mongodb'), // 3rd party…
0
votes
3 answers

Unit Testing: Constructor Injection with Fake Objects - Bad Tests?

In The Art of Unit Testing, 2nd Ed., the author gives the following example for injecting a stub using constructor injection and a "fake object". The goal of the "fake object" is to inherit the interface and break dependencies so that way you can…
8protons
  • 1,359
  • 1
  • 10
  • 27
0
votes
1 answer

When to use stubs

I'm trying to get into TDD, and a lot of examples sugests that we should use stubs to make our code more flexible. If I'm using javascript (for example) then why should I use stubs, since methods and even whole objects can be easily replaced for a…
0
votes
2 answers

Best practice to return a long string in a stub

I have some methods im trying to test which use a StreamReader dependency that i mock using Rhino mocks. But it is a large xmlFile. In the spirit of unit testing im trying to keep away from using a test file in my test project. On the other hand…
Mr. MonoChrome
  • 195
  • 1
  • 4
1
2