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 have a cart object which I am mocking, and I want clients to get a particular items collection, that is why I have the following statement while mocking the cart.

mock.ExpectGetAlways("Items",c.Items);

but when the client tries to access the property it is given empty collection.

Is there anything which I am missing


Following is the complete code

// get cart data
Cart c = CartObject.Create();
// init mock manager
MockManager.Init();
// mock cart
mock = MockManager.Mock(typeof(Cart));
// configure mock
mock.ExpectGetAlways("Items",c.Items);


thanks in advance.
asked by rxraza (1.8k points)

6 Answers

0 votes
Hi,

I have a cart object which I am mocking, and I want clients to get a particular items collection, that is why I have the following statement while mocking the cart.

mock.ExpectGetAlways("Items",c.Items);


what is the type of Items?
answered by richard (3.9k points)
0 votes
Hi,
I am trying to understand the code.
Do you have two instances of Cart?
Is one is mocked and returns the value of the other, that isn't mocked?
answered by scott (32k points)
0 votes
Scott:

Your understanding is correct. Items is one of the properties in Cart object and it's type is LineItemCollection.
answered by rxraza (1.8k points)
0 votes
Hi,
I have tried this case, and it works, TypeMock returns the correct value.

Probrably c.Items is null at the time of setting the expectation.
If you want the mocked Items to return the value of c.Items dynamiclly according to its value at runtime, you can use DynamicReturnValue as follows:

private Cart c;
public object ReturnItems(object[] parameters, object context)
{
     return c.Items;
}
[Test]
public void TheTest()
{
     // get cart data
     c = CartObject.Create();
     // init mock manager
     MockManager.Init();
     // mock cart
     mock = MockManager.Mock(typeof(Cart));
     // configure mock
     mock.ExpectGetAlways("Items",new DymanicReturnValue(ReturnItems));
}
answered by scott (32k points)
0 votes
Scott:

I guess in my case it is not happening. Here is the code complete.

private Cart _c = new Cart();

// it is guaranteed that when a cart is created like this, the Items Collection is populated with one item

this._c = CartObject.Create();

public object GetItems(object[] parameters, object context)
{
return _c.Items;
}

/// <summary>
/// Test the Delete Function
/// </summary>
[Test]
public void TestDelete()
{

// init mock manager
MockManager.Init();
// mock cart
Mock mock = MockManager.Mock(typeof(Cart));

mock.ExpectGetAlways("Items",new DynamicReturnValue(GetItems));
// configure mock
//Configure(c);

// the following instance would be mocked.
// it is interesting in TypeMock that future/next instances can be mocked.

cart = new Cart();
LineItemCollection items = cart.Items;
//items stll contain nothing

Is there anything that I am not doing ok?
answered by rxraza (1.8k points)
+1 vote
Hi,
Lets put a little code to see what is happening.
Add a small assert that will fail if _c.Items is null.

public object GetItems(object[] parameters, object context)
{
   Assert.IsNotNull(_c.Items,"Woops, _c.Items is null");
   return _c.Items;
} 


You can also check what happens if you return a new LineItemCollection as follows:
LineItemCollection retCollection = new LineItemCollection();
retCollection.Add(...);
mock.ExpectGetAlways("Items",retCollection);
answered by scott (32k points)
...