Stubbing is modifying an instance method or property of an object at runtime to extend its functionality.
Questions tagged [stubbing]
420 questions
21
votes
1 answer
Correct way to add helper functions for an rspec spec
So I need a helper function to created 'unprocessed tweets' similar to how I might receive them from the Twitter API gem, so I can test my models functionality under certain conditions.
To do this I added a helper function inside my objects…

NNNNNNNNNNDelicious
- 933
- 3
- 7
- 19
21
votes
4 answers
Mockito: how to stub void methods to run some code when called
I want to stub a repository class to test another class (Holder class) that has a repository. The repository interface supports CRUD operations, and has many methods, but my unit test on the Holder class only needs to call two of them. The…

Mister Smith
- 27,417
- 21
- 110
- 193
21
votes
1 answer
stub method only on the first call with Rspec
How can I stub a method only on the first call, and in the second one it should behave as expected?
I have the following method:
def method
do_stuff
rescue => MyException
sleep rand
retry
end
I want to the first call of do_stuff to raise…

fotanus
- 19,618
- 13
- 77
- 111
19
votes
4 answers
Stubbing with Wiremock - WithBodyFile Location other than _files
Wiremock Documentation states that the location of the file specified in withBodyFile should be in src/test/resources/__files. I would like to have file in src/test/resources/Testing_ABC/Testcase2/myfile.xml.
Is there any way I can achieve this ? I…

Ragini
- 1,509
- 7
- 28
- 42
18
votes
3 answers
Stubbing defaults in Mockito
How can I stub a method such that when given a value I'm not expecting, it returns a default value?
For example:
Map map =…

Alex Spurling
- 54,094
- 23
- 70
- 76
18
votes
1 answer
Alter Mock object after .Object property has been called
I am currently writing unit tests and mocking a dependency using Moq framework. In doing this I have created a Mock like so:
Mock traceProviderMock = new Mock();
traceProviderMock.Setup(x =>…

andrew_scfc
- 645
- 3
- 16
18
votes
3 answers
How to stub error raising using Rspec in Rails?
I'm new to Rails and Rspec and I'm using Rspec to test this controller method which includes exception handling:
def search_movies_director
@current_movie = Movie.find(params[:id])
begin
@movies = Movie.find_movies_director(params[:id])
…

Marina K
- 335
- 1
- 3
- 11
16
votes
4 answers
Mockito re-stub method already stubbed with thenthrow
I ran into a problem with mockito.
I am developing a web application. In my tests the user management is mocked.
There are some cases when I have to alter the User returned by the getLoggedInUser() method.
The problem is, that my getLoggedInUser()…

Sobvan
- 1,376
- 1
- 15
- 24
16
votes
2 answers
Rspec -- need to stub File.open that gets called in another file
In my test I'm initializing a new class called Package with some parameters.
In the initialization of this class, I open a file that is available on my remote boxes but not something that is commonly there locally. I was wondering how I would go…

Shail Patel
- 1,764
- 5
- 30
- 46
16
votes
2 answers
What is the difference between rhino-mocks stub and expect
What is the difference between rhino-mocks stub and expect here: Looks to me that they behave exact the same?
mockContext.Stub(x => x.Find())
.Return(new List()
{
new Blog() { Id = 1, Title = "Test" }
…

Nikos
- 7,295
- 7
- 52
- 88
15
votes
2 answers
How to test a function which takes a block with rspec
I have a function, which accepts a block, opens a file, yields and returns:
def start &block
.....do some stuff
File.open("filename", "w") do |f|
f.write("something")
....do some more stuff
yield
end
end
I am…

constantine1
- 427
- 1
- 5
- 12
14
votes
2 answers
MOQ stubbing property value on "Any" object
I'm working on some code that follows a pattern of encapsulating all arguments to a method as a "request" object and returning a "response" object. However, this has produced some problems when it comes to mocking with MOQ. For example:
public…

bytedev
- 8,252
- 4
- 48
- 56
13
votes
1 answer
Mockito - Stubbing a method of an object that was returned by a mock object method
Let's say I have an mock object, and I don't want to stub any of it's methods, but I want to stub a method of an object it returns. For example,
when(mockObject.method1()).thenReturn(returnValue)
is how it's normally done, but I'm looking…

gsgx
- 12,020
- 25
- 98
- 149
13
votes
1 answer
getting the object passed as an argument to a stubbed method with Mocha
Foo.expects(:bar)
Foo.bar(:abc => 123, :xyz => 987)
# assert Foo.bar was called with a hash that has a key of :abc == 123
Basically I want to examine the object passed as an argument to a stubbed method, in order to inspect on a value of that…

Alex Wayne
- 178,991
- 47
- 309
- 337
13
votes
8 answers
Stubbing Paperclip S3 requests in specs
I am using Paperclip and S3 for image uploads and am trying to stub out calls to S3 from my test suite. I found the thoughtbot post which mentions doing
a.cover { a.paperclip_fixture('album', 'cover', 'png') }
but that gives me a "wrong…

Eric M.
- 5,399
- 6
- 41
- 67