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
I'm working on mocking an object that would be loaded from a database and injecting what should be returned via the various properties in use.

This is what I have so far:

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
      ContentBlock mockedBlock = new ContentBlock();
      recorder.ExpectAndReturn(mockedBlock.EkItem, new Ektron.Cms.Common.ContentBase()).RepeatAlways();
      recorder.ExpectAndReturn(mockedBlock.Title, "Hello World Test");
      recorder.CheckArguments();
}


This all works perfectly but I need to add one more return value that just so happens to be a property of the EkItem object. The updated test looks like this:

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
      ContentBlock mockedBlock = new ContentBlock();
      recorder.ExpectAndReturn(mockedBlock.EkItem, new Ektron.Cms.Common.ContentBase()).RepeatAlways();
      recorder.ExpectAndReturn(mockedBlock.Title, "Hello World Test");
      recorder.ExpectAndReturn(mockedBlock.EkItem.Html, "<p>Hello World!</p>");
      recorder.CheckArguments();
}


The error that I'm getting is this:

TestCase 'NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml'
failed: TypeMock.TypeMockException :
*** Method get_EkItem in type Ektron.Cms.Controls.ContentBlock cannot return System.String
at TypeMock.RecordExpectations.Return(Object returnValue)
at TypeMock.RecordExpectations.ExpectAndReturn(Object mockedStatements, Object returnValue)
C:clientWorkNationalFinancialBACsrcTests.UnitCoreModelsXmlSmartFormBlockBehaviour.cs(31,0): at NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml()


What am I doing wrong?
asked by kyleheon (3.1k points)

7 Answers

0 votes
Hi,
Please try to mock just the Html property.
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
      recorder.ExpectAndReturn(new ContentBlock().EkItem.Html, "<p>Hello World!</p>");
      recorder.CheckArguments();
}


Also try to mock ContentBase.
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
      ContentBase mockedBase = new Ektron.Cms.Common.ContentBase()
      ContentBlock mockedBlock = new ContentBlock();
      recorder.ExpectAndReturn(mockedBlock.EkItem,mockedBase).RepeatAlways();
      recorder.ExpectAndReturn(mockedBlock.Title, "Hello World Test");
      recorder.ExpectAndReturn(mockedBase.Html, "<p>Hello World!</p>");
      recorder.CheckArguments();
}


Post if these run and we will see if it a bug or not.
answered by scott (32k points)
0 votes
Scott,

Thanks for the tips. I have tried both and neither of them were successful. Here is what I experienced:

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
      recorder.ExpectAndReturn(new ContentBlock().EkItem.Html, "<p>Hello World!</p>");
      recorder.CheckArguments();
}


This failed on the recorder.ExpectAndReturn line with the following error:

TestCase 'NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml' failed: TypeMock.TypeMockException:
*** Method get_EkItem in type Ektron.Cms.Controls.ContentBlock cannot return System.String
at TypeMock.RecordExpectations.Return(Object returnValue)
at TypeMock.RecordExpectations.ExpectAndReturn(Object mockedStatements, Object returnValue)
C:clientWorkNationalFinancialBACsrcTests.UnitCoreModelsXmlSmartFormBlockBehaviour.cs(37,0): at NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml()


The second sample (listed below) is something I'd already tried before posting but tried again for good measure.

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
   Ektron.Cms.Common.ContentBase mockedBase = new Ektron.Cms.Common.ContentBase();
   ContentBlock mockedBlock = new ContentBlock();
   recorder.ExpectAndReturn(mockedBlock.EkItem, mockedBase).RepeatAlways();
   recorder.ExpectAndReturn(mockedBlock.Title, "Hello World Test");
   recorder.ExpectAndReturn(mockedBase.Html, "<p>Hello World!</p>");
   recorder.CheckArguments();
}


This sample fails with the following error:

TestCase 'NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml' failed: TypeMock.TypeMockException:
*** Method get_Title in type Ektron.Cms.Controls.EkXsltWebPart already has a mocked return value
Perhaps you are trying to mock a method from mscorlib
at TypeMock.RecordExpectations.Return(Object returnValue)
at TypeMock.RecordExpectations.ExpectAndReturn(Object mockedStatements, Object returnValue)
C:clientWorkNationalFinancialBACsrcTests.UnitCoreModelsXmlSmartFormBlockBehaviour.cs(47,0): at NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml()
.

Let me know what you need more from me. Any other tips for how I might be able to get this to work are greatly appreciated. I can't figure out another way to test this particular code.
answered by kyleheon (3.1k points)
0 votes
Just in case you are wondering, I also tried this using Reflective Mocks and it doesn't work that way either.
answered by kyleheon (3.1k points)
0 votes
According to the samples it seems that TypeMock is not able to register the call to the Html property.
This might have something to do with the definition of the Html property.

1. What type does EkItem return?
2. In what type is Html defined?
3. Could you post the definition of the Html property?
4. What is the error code for the reflective mocks?
answered by scott (32k points)
0 votes
Answers to your questions:

1. What type does EkItem return?

EkItem is of type Ektron.Cms.Common.ContentBase

2. In what type is Html defined?

Html is a string property of ContentBase (made available via EkItem in ContentBlock()).

3. Could you post the definition of the Html property?

Not really sure if I can because it's a commercial product. I used Reflector to look inside but the only definition I see is:
[XmlElement]
public string Html;


The Reflected source for EkItem from ContentBlock looks like this:
[Browsable(false)]
public ContentBase EkItem
{
    get
    {
        return this._Item;
    }
}


4. What is the error code for the reflective mocks?

Using the following code:
Mock mock = MockManager.Mock<Ektron.Cms.Common.ContentBase>();
mock.ExpectGet("Html", "<p>Hello World!</p>");


I'm getting an error like this:
TestCase 'NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml' failed: TypeMock.TypeMockException:
*** No method get_Html in type Ektron.Cms.Common.ContentBase
at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Int32 A_4, Type[] A_5)
at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Int32 timesToRun, Type[] genericTypes)
at TypeMock.Mock.ExpectGet(String property, Object value, Int32 timesToRun)
at TypeMock.Mock.ExpectGet(String property, Object value)
C:clientWorkNationalFinancialBACsrcTests.UnitCoreModelsXmlSmartFormBlockBehaviour.cs(52,0): at NationalFinancial.BAC.Tests.Unit.Core.Models.XmlSmartFormBlockBehaviour.ShouldCreateMatchingXml()
answered by kyleheon (3.1k points)
0 votes
Ok,
There is our answer.
Html is a field and not a Property.
Currently TypeMock can not mock fields.
But as it is a field you can simply set it.
new ContentBlock().EkItem.Html = "<p>Hello World!</p>"
answered by scott (32k points)
0 votes
Thank you, that did the trick.
answered by kyleheon (3.1k points)
...