This is a migrated thread and some comments may be shown as answers.

Mocking Constructor ?

3 Answers 219 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 07 Sep 2011, 03:47 PM

I am just starting with JustMock and have a question related to your example in documentation on this page:

    http://www.telerik.com/help/justmock/basic-usage-mock-internal-types-via-proxy.html

I have classes similar to this sample ( internal is not important here it could be public ):

internal class FooInternal
{
    internal FooInternal()
    {
        builder = new StringBuilder();
    }
      
    public StringBuilder Builder
    {
        get
        {
            return builder;
        }
    }
      
    private StringBuilder builder; 
}

What I would like to do is to get my class (FooInternal in this case) to instanciate a Mock of another class (StringBuilder here)...

The only way I can see this possible is by mocking the (FooInternal) constructor itself. Is this possible ?

Thanks !


3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 09 Sep 2011, 09:16 AM
Hi Michel,
Thanks again for bringing the question. Here what you can do is to mock the constructor first then arrange the StringBuilder with your custom one. Therefore, you could write the test in the following way :

var foo = Mock.Create<FooInternal>(Constructor.Mocked);
 
var stringBuilder = new StringBuilder("Custom builder");
 
Mock.Arrange(()=> foo.Builder).Returns(stringBuilder);
 
Assert.AreEqual(foo.Builder, stringBuilder);

Since foo.Builder is non-virutal, here you would need to have the profiler enabled. But in your case if you want to mock the following line in the constructor itself:

builder = new StringBuilder();

It is generally not possible, since you then have to pass that builder instance to the getter as well.


Hope that answers your question.

Kind Regards,
Mehfuz
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Michel
Top achievements
Rank 1
answered on 09 Sep 2011, 01:39 PM
Thank you !

I just learned this JustMock magic from someone more knowledgeable about JustMock...

I am used to building my own mock classes by hand and using Unity injection to register all mock classes ahead of time...

Now I see all the power of using your framework, all the time saved is much apreciated !

Thanks again,

Michel
0
Ricky
Telerik team
answered on 09 Sep 2011, 01:44 PM
Hi Michel,
Thanks again for the reply and you are most welcome !

Kind Regards,
Mehfuz
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
General Discussions
Asked by
Michel
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Michel
Top achievements
Rank 1
Share this question
or