Mock the Next Instance of an Object Type?

4 posts, 0 answers
  1. cecilUOA
    cecilUOA avatar
    20 posts
    Member since:
    May 2007

    Posted 28 Nov 2011 Link to this post

    In my code I create a SharePoint site collection object (SPSite) using a url.
    using (var siteCollection = new SPSite(spWebAppUri + siteUrl))
    // more code...

    So I want to mock that site collection object, but I dont know how to swap out a thing like that which is not referenced to anything that I can call statically like SPContext.Current.Web. In TypeMock it would be something like this...

    // the next time a new instance of SPSite is created, use our fake one instead  

    Thank you
  2. Ricky
    Ricky avatar
    467 posts

    Posted 01 Dec 2011 Link to this post

    Hi Doug,

    Thanks again for asking the question. Here you can use the "IgnoreInstance" feature that will let you execute a specific mock setup without passing the instance via dependency injection.

    Let’s take a look at the following example ::

    var fakeUsed = Mock.Create<UsedClass>();
    Mock.Arrange(() => fakeUsed.ReturnFive()).Returns(7).IgnoreInstance();
    Assert.AreEqual(7, new UsedClass().ReturnFive());

    As we have applied IgnoreInstance switch for ReturnFive() method; despite creating a new instance of UsedClass, ReturnFive() is still returning the expected result.

    Hope this will solve your problem.

    Kind Regards.
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Robert
    Robert avatar
    3 posts
    Member since:
    Jun 2010

    Posted 20 Aug 2012 Link to this post

    I came here to ask this exact same question. 

    So is there no way to accomplish this without having to explicitly arrange every method that will be mocked? I would like to completely avoid the instantiation of the real object and only use the mock.

    I'm looking for something like:
    var mockItem = Mock.Create<Item>(Behavior.Loose);
    Mock.Arrange(() =>
    new Item()).Returns(mockItem).IgnoreInstance();
    var sutItem = SomeMethodThatCreatesAndReturnsAnItem();
    Assert.AreEqual(mockItem, sutItem);

    The problem is that the constructor of Item requires some dependencies that I would like to avoid altogether.

    I've looked through all the docs and code samples and can't see any examples of doing this, which I would think would be highly desired behavior.
  4. Ricky
    Ricky avatar
    467 posts

    Posted 23 Aug 2012 Link to this post

    Hi Doug,
    Thanks again for contacting us.

    Currently, Ignore instance is supported per method call, which makes it explicit for which call you want to ignore the instance and also minimizes any performance overhead.

    In Q2 2012 SP1, we added a feature that will let you skip the constructor call if you have specified Constructor.Mocked for the mocked instance and arranged a method that you want to execute regardless of its instance boundary.

    Let's consider the following example:
    var foo = Mock.Create<Foo>(Constructor.Mocked);
    Mock.Arrange(() => foo.Echo("S")).Returns((s) = > s).IgnoreInstance();
    // provided that constructor throws exception the following will pass
    new Foo().Echo("S");

    However, we understand your point and we have a plan to include it in Q3 release. You can in the meantime track the progress of the task here: 
    Kind Regards
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top