Mocking Constructor ?

4 posts, 0 answers
  1. Michel
    Michel avatar
    39 posts
    Member since:
    Jul 2011

    Posted 07 Sep 2011 Link to this post

    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 !


  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 09 Sep 2011 Link to this post

    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 >>

  3. DevCraft R3 2016 release webinar banner
  4. Michel
    Michel avatar
    39 posts
    Member since:
    Jul 2011

    Posted 09 Sep 2011 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 09 Sep 2011 Link to this post

    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 >>

Back to Top