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

Mock new SPSite(“http://someURL“)

3 Answers 129 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 22 Jun 2012, 07:28 PM

Hello,

this question has been ask here before, but I do not see how the answer is of any use in this case.

I want to test a method where I have something like this:

public string DoSomething()
        {        
            using (SPSite site = new SPSite("http://someURL"))
            {
                using (SPWeb web = site.OpenWeb())
                {

 

At this time the URL does not exist.

In the test case I create a mock for SPSite and SPWeb (SharePoint objects).

var spSite = Mock.Create<SPSite>();       
var spWeb = Mock.Create<SPWeb>();
Mock.Arrange(() => spSite.OpenWeb()).Returns(spWeb);

But I get this exception in the mehtod under test when the new SPSite is created:

System.IO.FileNotFoundException: The Web application at http://someUrl could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

How can I use the mock spSite when new SPSite("http://someURL")) is called?
IgnoreInstance only swaps method calls and does not work with constructor calls. (?)

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 28 Jun 2012, 10:47 AM
Hi Christian,

Thanks for bringing up the question. 

For faking the constructor you can use something like this:

var spSite = Mock.Create<SPSite>(Constructor.Mocked);

This will ensure that the original constructor is not called and therefore you won’t get the exception.

Kind Regards
Ricky
the Telerik team

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

0
Chris
Top achievements
Rank 1
answered on 29 Jun 2012, 11:49 AM

This only prevents the call to the default constructor. I still get the same exception because the original non default constructor is still called.

0
Accepted
Ricky
Telerik team
answered on 05 Jul 2012, 04:43 PM
Hi Christian,
Thanks again for contacting us.

When you mock a constructor, it fakes the call for current instance. So here you are creating a new instance of SpSite which will call the original constructor anyway. However, we have made a new update recently that will let you skip the constructor call if you apply IgnoreInstance for the member of a target type during Mock.Arrange. For example:

var spSite = Mock.Create<SPSite>(Constructor.Mocked);
 
Mock.Arrange(() => spSite.AllowRssFeeds).IgnoreInstance().Returns(true);
 
using (var site = new SPSite("www.yahoo.com"))
{
    Assert.IsTrue(site.AllowRssFeeds);
}


Here the new on SpSite won’t invoke the original constructor. If you can open up a support ticket then I can send you the latest internal build containing the update.

Kind Regards
Mehfuz
the Telerik team

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

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