I need to be able to specify a collection of Titles. I've tried everything I can think of but nothing has worked. The latest code I've tried is:
9 Answers, 1 is accepted
Mock.Arrange( () => mockList.Title)).Returnsa("fakeTitle");
Mock.Arrange( () => mockList.Title).Returns("fakeTitle");
This was just an error in the forum post - my code in VS doesn't have the typo. :-)
I'm not anywhere close to being a Sharepoint developer, so I need to ask a few questions;
What is a "SUT"?
Are you trying to mock the SUT or test the SUT?
Are you using JustMock Lite or the commercial version of JustMock?
Is ClientContext a class you defined or part of the Sharepoint objet
SUT is just the class I'm testing. It's a custom class I developed that for now just contains the "GetListTitles" method at the top of my post. I'm trying to test the GetListTitles method inside it. GetListTitles uses the .NET SharePoint Client Side Object Model (.NET CSOM) to enable calls back to SharePoint from off-server. ClientContext is a class inside CSOM that manages the connection back to SP.
I'm successfully able to mock ClientContext. The problem I'm having is that when GetTitles calls ctx.Web.Lists, I need to have my mocked ListCollection returned so that I can control the contents of the collection. I'm mocking the ListCollection in my test but I can't get it to be the return value for the Lists property of my mocked Web.
Does that make any sense?
I can see that you are creating a mock of ClientContext and using the Constructor.Mocked argument. That will tell JustMock that you want to mock the constructor. To have the methods of the concrete class be replaced with the arrangements you need to use the IgnoreInstance method when you do the arrangement.
Mock.Arrange(() => mockCtx.Web).Returns(mockWeb);
Mock.Arrange(() => mockCtx.Web).IgnoreInstance().Returns(mockWeb);
You can leave the Constructor.Mocked argument if you like, but based on this particular snippet of code I don't think you need it. Future mocking is part of JustMock commercial, so you need to make sure the profiler is enabled when you run the test. You can find more information about future mocking on the JustMock future mocking documentation page(http://www.telerik.com/help/justmock/advanced-usage-future-mocking.html).
This should work. If not please let me know we can dig a little deeper.
But it doesn't seem to be working.
Or, maybe the problem is when the code in my GetListTitles method tries to iterate the mocked ListCollection something fails. How can I tell if my mocked ListCollection is actually being returned?
I also tried mocking the GetEnumerator of the ListCollection but that failed miserably.
In order to achieve this, you will need to use the JustMocks ReturnsCollection feature as shown here for example.
Here is the code that worked on my side:
Please, give it a try and let me know if there are further issues.
I hope it helps.
DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!
The final test code looked like this:
Thanks again for the help!
We are glad that your tests are working. If you need any further assistance, do not hesitate to write us again!
DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.