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

4 posts, 1 answers
  1. Chris
    Chris avatar
    2 posts
    Member since:
    Jun 2012

    Posted 22 Jun 2012 Link to this post

    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. (?)

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 28 Jun 2012 Link to this post

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

  3. DevCraft R3 2016 release webinar banner
  4. Chris
    Chris avatar
    2 posts
    Member since:
    Jun 2012

    Posted 29 Jun 2012 Link to this post

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

  5. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 05 Jul 2012 Link to this post

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

Back to Top