Some SharePoint faking questions

15 posts, 1 answers
  1. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 26 Jun 2013 Link to this post

    Hello I have a question about SharePoint Faking.
    in my Class unter Test I have an method that use following code.
     public Guid CreateNewSite(String siteUrl, string newSite)
    {

    using
    (SPSite site = new SPSite(siteUrl))
                    {
                        if (!site.AllWebs.Names.Contains(newSite))
                        {
                            return site.AllWebs.Add(newSite, newSite, "New Website", 1033, "STS#1", true, false).ID;
                        }
    } 
    and this is my Testcode:
    SPSite fakeSite = Mock.Create<SPSite>();
              Mock.Arrange(() => fakeSite.AllWebs.Names).ReturnsCollection<string>(new[] { "nonExistsWeb" });
              Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, (uint)Arg.AnyInt, Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(SomeGuid);
     
    Guid actual = spListCreator.CreateNewSite("http://local""SomeSite");
    But this will not work. I get this error:
    System.IO.FileNotFoundException: The Web application at http://local 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.

    Why?


    Regards
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 28 Jun 2013 Link to this post

    Hello Artiom,

    Your code is calling the original constructor of SPSite in:
    using (SPSite site = new SPSite(siteUrl))

    which I think throws that exception. You need to mock it as well. Try arranging that constructor to do nothing:
    Mock.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();



    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 28 Jun 2013 Link to this post

    Sorry, but I don't unterstand how I can do it.
    Can you give me an example based on my code?
  5. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 28 Jun 2013 Link to this post

    Hi Artiom,

    Add this line to the arrangements of your test code:
    Mock.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();

    This line makes the SPSite constructor do nothing, instead of trying to look for the site given in its first argument.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 28 Jun 2013 Link to this post

    Now it works, but I have to do more work.

    Mock.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();
              SPSite fakeSite = Mock.Create<SPSite>();
              Mock.Arrange(() => fakeSite.AllWebs.Names).IgnoreInstance().ReturnsCollection<string>(new[] { "nonExistsWeb" });
              SPWeb fakeWeb = Mock.Create<SPWeb>();
              Mock.Arrange(() => fakeWeb.ID).Returns(webGuid);
              Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, (uint)Arg.AnyObject, Arg.AnyString, Arg.AnyBool, Arg.AnyBool)).IgnoreInstance().Returns(fakeWeb);

    Is it right, that Justmock doesn't support recursive mocking?
    With Typemock this example will looks like this:

    SPSite fakeSite = Isolate.Fake.NextInstance<SPSite>();
     
                Isolate.WhenCalled(() => fakeSite.AllWebs.Names).WillReturn(new[] { "nonExistsWeb" });
     
                Isolate.WhenCalled(() => fakeSite.AllWebs.Add(string.Empty, string.Empty, string.Empty, 0, string.Empty, false, false).ID).WillReturn(webGuid);

    How can I reduce my code with Justmock?

  7. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 01 Jul 2013 Link to this post

    Hello Artiom,

    JustMock supports recursive mocking inside Mock.Arrange statements. In other words, the following
    statement works:

    Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, (uint)Arg.AnyInt, Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(SomeGuid);
    i.e, it will arrange fakeSite.AllWebs to return an auto-created mock and that whenever the Add method is invoked on the fakeSite.AllWebs, then the return value will be auto-arranged to return a mock and its ID will be SomeGuid. However, there's no direct equivalent to TypeMock's Fake.NextInstance<SPSite>(); API. In JustMock you need to use the IgnoreInstance() API, just like you do in your example.

    Here's a shortened version of your example:
    var allWebs = Mock.Create<SPWebCollection>();
    Mock.Arrange(() => allWebs.Names).ReturnsCollection<string>(new[] { "nonExistsWeb" });
    Mock.Arrange(() => allWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.IsAny<uint>(), Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(webGuid);
     
    Mock.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();
    Mock.Arrange(() => new SPSite(Arg.AnyString).AllWebs).Returns(allWebs);

    I explicitly create and arrange a mock of SPWebCollection, and then arrange that the AllWebs property of any SPSite (ignoring instance) will return my mock. The syntax Mock.Arrange(() => new SPSite(Arg.AnyString).AllWebs) is equivalent to arranging the AllWebs property on a specific instance of SPSite and then adding .IgnoreInstance() to that arrangement.

    I hope this helps you with your current situation. If you have any other questions, please, don't hesitate to write us back.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  8. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 01 Jul 2013 Link to this post

    "JustMock supports recursive mocking inside Mock.Arrange statements."
    Does it mean, that when I create i.e. some fake object with Mock.Create<XXX> I will not get a Recursive Fakes object, Like I get those with Typemock (Return Recursive Fakes)?

    You says that this works
    Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, (uint)Arg.AnyInt, Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(SomeGuid);
    but I doesn't.
    After
    Mock.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();
              SPSite fakeSite = Mock.Create<SPSite>();
    in debugger I can see in fakeSite.AllWebs the fake SPWebCollection, but when I call the
    fakeSite.AllWebs.Add(string.Empty, string.Empty, string.Empty, 0, string.Empty, false, false);
    method manually, I get NULL back.
    It seems (for me) that the JustMock can only generate fakes only at 1 level.

    Is it correct?
  9. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 03 Jul 2013 Link to this post

    Hello Artiom,

    JustMock supports recursive mocking with any level of nesting, not just one. Here is the test I tried:

    //Arrange
    var site = Mock.Create<SPSite>();
    var guid = Guid.NewGuid();
    Mock.Arrange(() => site.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.IsAny<uint>(), Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(guid);
     
    //Act
    var web = site.AllWebs.Add(null, null, null, 0, null, false, false);
     
    //Assert
    Assert.Equal(web.ID, guid);

    The test passed.

    Could you share a complete snippet that reproduces your issue? I will be better able to help you this way.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  10. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 03 Jul 2013 Link to this post

    double posted...
  11. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 03 Jul 2013 Link to this post

    Hello Stefan,

    here is my Code:
    public Guid CreateNewSite(String siteUrl, string newSite)
    {
        using (SPSite site = new SPSite(siteUrl))
        {
            if (!site.AllWebs.Names.Contains(newSite))
            {
                return site.AllWebs.Add(newSite, newSite, "New Website", 1033, "STS#1", true, false).ID;
            }
        }
        return Guid.Empty;
    }

    and here my test code that not works:
    //Arrange
    SharePointListCreator spListCreator = new SharePointListCreator();
    Guid webGuid = new Guid("371FCC0B-51A8-4DDC-884F-FD1A48CBCF52");
    Mo
    ck.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();
    SPSite fakeSite = Mock.Create<SPSite>();
    Mock.Arrange(() => fakeSite.AllWebs.Names).IgnoreInstance().ReturnsCollection<string>(new[] { "nonExistsWeb" });
    Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.IsAny<uint>(), Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(webGuid);
     
    //Act
    Guid actual = spListCreator.CreateNewSite("http://local", "testWeb");
     
    //Assert
    Assert.AreEqual(webGuid, actual);
  12. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 04 Jul 2013 Link to this post

    Hi Artiom,

    The reason that this line of code:
    Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.IsAny<uint>(), Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(webGuid);
    doesn't work is that it is an arrangement made for 'fakeSite' only. You need to make that arrangement a future one, so that it works for every instance of SPSite henceforth. There is no direct way to do this with the JustMock API. What you can do is apply all arrangements to a mock of SPWebCollection and then using future mocking arrange that all SPSite instances will return it for its AllWebs property. I'm referring you to an example from this thread:
    // create mock of AllWebs
    var allWebs = Mock.Create<SPWebCollection>();
    Mock.Arrange(() => allWebs.Names).ReturnsCollection<string>(new[] { "nonExistsWeb" });
    Mock.Arrange(() => allWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.IsAny<uint>(), Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(webGuid);
      
    // use future-mocking to arrange the AllWebs property for all SPSite instances
    Mock.Arrange(() => new SPSite(Arg.AnyString)).DoNothing();
    var fakeSite = Mock.Create<SPSite>();
    Mock.Arrange(() => fakeSite.AllWebs).IgnoreInstance().Returns(allWebs);


    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  13. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 04 Jul 2013 Link to this post

    Hello Stefan and thanx for your answer.

    Your solution looks like mine (see my 3rd post).
    Is it a bug or unimplemented function, that I can't  do this with JustMock APi?
    I mean will it be aviable in the future?
    Are there more limitations like this?

    Regards
  14. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 05 Jul 2013 Link to this post

    Hi Artiom,

    It appears to be a limitation of the current API.

    I've added a feature request to our feedback portal. You can check it out and vote for it and comment on it here.

    I can't say when it will be done, because first we'll need to prioritize it with respect to all other features we're working on this Q. If the lack of this feature in JustMock is a deal-breaker for you, then let me know.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  15. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 05 Jul 2013 Link to this post

    Hello Stefan.

    Thanks for your reply.
    If you know about this limitation, you can handle it. I don't know yet if this limitation is a deal breaker for me, because there can be more such limitations. So I will test justmock a bit more. But of course it would be nicer, not to have this limitation. It is always better to save time or additional code line :)

  16. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 05 Jul 2013 Link to this post

    Hello Artiom,

    If there's anything else that I can help you with during your trial, write me back. I'm here to help in any way I can.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top
DevCraft R3 2016 release webinar banner