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

Mock the Next Instance of an Object Type?

3 Answers 119 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
cecilUOA
Top achievements
Rank 1
cecilUOA asked on 29 Nov 2011, 01:40 AM
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  
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);

Thank you

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 01 Dec 2011, 12:57 PM
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.
Mehfuz
the Telerik team

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

0
Robert
Top achievements
Rank 1
answered on 20 Aug 2012, 03:58 PM
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.
0
Ricky
Telerik team
answered on 23 Aug 2012, 05:49 PM
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:

http://www.telerik.com/support/pits.aspx#/public/justmock/12418 
 
Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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