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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi. I'm have a mocked interface with properties that are being called by another object. Is there any way I can easily retrieve what value my object pushed into that interface's property? The property's value would be a list of objects. I guess I could use a custom argument checker for this, but is there a simpler way? From the sparse docs (I'm using the 4.2.1 beta version) it looks like the ReturnDefaultImplementation method might accomplish this, but it doesn't seem to affect anything for me. What I really want to do can be accomplished in Rhino Mocks with the "PropertyBehavior" concept (Expect.Call(demo.Prop).PropertyBehavior();). Is there a comparable feature in TypeMock?
asked by cfarmerga (1.8k points)

4 Answers

0 votes
Hi,

Typemock has several mechanisms for acccessing arguments passed into methods (or properties).
Argument checking is one, dynamic return values is another way that will allow you to manipulate the passed arguments.

You can also use the MockMethodCalled event, which allows you to catch all calls made on mocked methods. (for further information see here.)

:idea: That being said, I cant say I fully understood what you are trying to do. So if you can further explain (or even give an example) I'm sure we can find a suitbale way to achieve what you need.
answered by lior (13.2k points)
0 votes
Hi. I have a MVP-type pattern and I'm trying to test the presenter here. I create a mocked view instance and pass it to the constructor of the presenter. The presenter passes some value to the view that I would like to be able to retrieve from the view and use elsewhere. I don't really care what this value is, but I will need it later to verify my test. In Rhino Mocks, this is exceedingly easy to do:

[Test]
public void PropertyBehaviorForSingleProperty()
{
  Expect.Call(demo.Prop).PropertyBehavior();
  mocks.ReplayAll();
  for (int i = 0; i < 49; i++)
  {
    demo.Prop = "ayende" + i;
    Assert.AreEqual("ayende" + i, demo.Prop);
  }
  mocks.VerifyAll();
}


The "PropertyBehavior" method in the expectation gives the mock's "Prop" property the ability to return the value that was placed in it. My presenter will fill the value, and I want to retrieve that value in my test to use it in verifying the test. It's possible to do what I need with a custom check, but that seems much harder to read (and more work to implement in the test), IMO.


thanks,
chris
answered by cfarmerga (1.8k points)
0 votes
Is my question strange because typemock doesn't typically *need* to have this special behavior since you can just delegate to the actual implementation of ordinary properties of object instances?

In my case, I just have an interface, but I'd like the mocked object representing this interface to simulate a simple get/set property that holds the value set to it.
answered by cfarmerga (1.8k points)
0 votes
:arrow: This feature is already implemented in Typemock - although it is written in the last sentences on the Mocking Interfaces Chapter (and here) and should be more visible.

All abstract methods and properties are created with a default implementation. By default, these methods are strict and will fail if they are called without any expectations. To use these you must change the strictness (see Arbitrary Calls).

Here is the default implementation:

* All void calls return
* Other methods will return a default value (0 or null depending on the return type)
* Properties will behave like simple properties


All you have to do is:
mock.Strict = false; // all properties work... 


So here is how you do it:
[Test,VerifyMocks]
public void PropertyBehaviorForAllProperties()
{
  MockObject<IDemo> mock = MockManager.MockObject<IDemo>();
  mock.Strict = false; // all properties work...

  IDemo demo = mock.Object; // this is our mocked instance
  for (int i = 0; i < 49; i++)
  {
    demo.Prop = "typemock" + i;
    Assert.AreEqual("typemock" + i, demo.Prop);
  }
} 


To use allow a specific property (public or private) use:
mock.MethodSettings("Prop").IsStrict = false;

:idea: As you said there is no need to do this for concrete classes/methods.
answered by eli (5.7k points)
...