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

Some SharePoint faking questions

14 Answers 58 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Artiom
Top achievements
Rank 1
Artiom asked on 26 Jun 2013, 03:58 PM
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

14 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 28 Jun 2013, 07:16 AM
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.
0
Artiom
Top achievements
Rank 1
answered on 28 Jun 2013, 11:53 AM
Sorry, but I don't unterstand how I can do it.
Can you give me an example based on my code?
0
Stefan
Telerik team
answered on 28 Jun 2013, 01:18 PM
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.
0
Artiom
Top achievements
Rank 1
answered on 28 Jun 2013, 01:56 PM

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?

0
Stefan
Telerik team
answered on 01 Jul 2013, 12:37 PM
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.
0
Artiom
Top achievements
Rank 1
answered on 01 Jul 2013, 02:32 PM
"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?
0
Stefan
Telerik team
answered on 03 Jul 2013, 07:06 AM
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.
0
Artiom
Top achievements
Rank 1
answered on 03 Jul 2013, 10:20 AM
double posted...
0
Artiom
Top achievements
Rank 1
answered on 03 Jul 2013, 11:08 AM
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);
0
Accepted
Stefan
Telerik team
answered on 04 Jul 2013, 02:37 PM
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.
0
Artiom
Top achievements
Rank 1
answered on 04 Jul 2013, 02:50 PM
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
0
Stefan
Telerik team
answered on 05 Jul 2013, 07:07 AM
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.
0
Artiom
Top achievements
Rank 1
answered on 05 Jul 2013, 12:15 PM
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 :)

0
Stefan
Telerik team
answered on 05 Jul 2013, 12:35 PM
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.
Tags
General Discussions
Asked by
Artiom
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Artiom
Top achievements
Rank 1
Share this question
or