chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I would like to mock replace the return value of a property on my test class. I have tried with the following code without success.

Dim MasterCollection As API.Base.OrderInfo.OrderMasters
MasterCollection = New API.Base.OrderInfo.OrderMasters("OrderMasters", Nothing)
Dim TestSubject As API.Base.OrderInfo.OrderMaster
TestSubject = New API.Base.OrderInfo.OrderMaster(MasterCollection)

MockManager.Init()
Dim OrderDetailsMock As MockObject
OrderDetailsMock = MockManager.MockObject(GetType(API.Base.OrderInfo.OrderDetails))
Dim OrderDetailMock As MockObject
OrderDetailMock = MockManager.MockObject(GetType(API.Base.OrderInfo.OrderDetail))
OrderDetailsMock.ExpectAndReturn("AddNew", OrderDetailMock.Object)

Using Recorder As New RecordExpectations
Dim Trash As Object = TestSubject.Details
Recorder.Return(OrderDetailsMock.Object)
End Using

Dim ProductOrderDetail As API.OrderInfo.IOrderDetail

ProductOrderDetail = TestSubject.AddProduct(45)


Is this possible? If so how should I be doing it?
asked by jflowers (1.5k points)

7 Answers

0 votes
So I take it back. That does work.

BTW sorry about the nasty formating, or lack there of.

So how can I do this all in natural mocking? Something that you should know about the property OrderMaster.Details, there is not backing field. So the ObjectState guy is not going to help here. :-(

There are going to be a lot of expectations on the OrderDetailMock so intellisense will really help.

Thanks
Jay
answered by jflowers (1.5k points)
0 votes
Hi
Sorry.
I'm not sure I understand the problem. :oops:
What's keeping you from inserting the mock
statements into the recording block?

Can you clarify this issue?
answered by ohad (35.4k points)
0 votes
I hope lack of understanding. Things always work out best when it is my fault as I am the easiest thing to fix/change.

Using Recorder As New RecordExpectations
   Dim OrderDetailsMock As New API.Base.OrderInfo.OrderDetails
   Dim OrderDetailMock As New API.Base.OrderInfo.OrderDetail
   Recorder.ExpectAndReturn(OrderDetailsMock.AddNew, OrderDetailMock)
   Recorder.ExpectAndReturn(TestSubject.Details, OrderDetailsMock)
   OrderDetailMock.SetPropertyAccess(False)
End Using

Dim ProductOrderDetail As API.OrderInfo.IOrderDetail

ProductOrderDetail = TestSubject.AddProduct(ProductId)


I have tried this expecting it to work. It fails because OrderDetails' constructor is called as a result of TestSubject.AddProduct. I am not all that familiar with the codebase that I am working with here. I think reflection might be being used to create a new OrderDetails.

Thanks,
Jay

P.S. How do you get code to print pretty in this forum?
answered by jflowers (1.5k points)
0 votes
Hi Jay

First to the formatting question:
Use the 'code' button before your posted code
and hit the 'close tags' link after the code ends. (both of them below the 'Subject' text box)
I edited your post so you can see it.
Just hit edit and look at the small change I made.

I think that the problem is that the constructor is mocked by default.
try using the Recorder.CallOriginal() method

Using Recorder As New RecordExpectations 
   Dim OrderDetailsMock As New API.Base.OrderInfo.OrderDetails 
   Recorder.CallOriginal()
   Dim OrderDetailMock As New API.Base.OrderInfo.OrderDetail 
   Recorder.ExpectAndReturn(OrderDetailsMock.AddNew, OrderDetailMock) 
   Recorder.ExpectAndReturn(TestSubject.Details, OrderDetailsMock) 
   OrderDetailMock.SetPropertyAccess(False) 
End Using 


Hope it helps.
answered by ohad (35.4k points)
0 votes
Hmm,
With out the CallOriginal I can set a breakpoint in the constructor and stop there.

When I use the MockManager, as in the first post, the contrustor is never called.

This may all be besides the point as I don't see any evidence that anything is being mocked when I try and do it with out the MockManager.

I want everything about OrderDetails and OrderDetail mocked.

Smiles,
Jay
answered by jflowers (1.5k points)
0 votes
hi

It seems that there is bit of confusion here.
Ohad and I have talked discussed about your issues, and we are still not 100% sure what is the exact problem that you see.
In general we assume that you want to test the OrderMaster.AddProduct method and you expect that during its call
1) a new OrderDetail object will be created.
2) a new OrderDetails(which is some sort of collection) object will be created.
3) the new OrderDetail instance will be added to the OrderDetails collection.
4) the details property of TestSubject will be called.

Anyway, since by default we expect constructors to be mocked you should be able to stop at a breakpoint in the OrderDetails' constructor (unless more then a single instance is created).
If you manage to do this then something is not right.

You also indicate that you get the impression that using Natural Mocks nothing is getting mocked. so i think that it will be best if we can verify that indeed some things are getting mocked there.

can you please activate the tracer during the test run, save the output and mail it to us? you should be able to see in there what is getting mocked and what is not.

Also you mentioned that you are new to this codebase, so it is important to notice that when mocking concrete classes (i.e. not interfaces and abstract) when an "unexpected" method is called the real code is activated.

Please let me knows if this help.
answered by lior (13.2k points)
0 votes
On the face of it I would guess this is due to the fact that I am using the default constructor in the Natural Mock Block and the test subject is using a different constructor.

I will try calling using the constructor with the same signature as is being called in the test subject. If that does not work I will try the tracing.

Thanks for all your help so far. :-)
Jay
answered by jflowers (1.5k points)
...