Telerik Forums
JustMock Forum
1 answer
100 views
Hi all,

I have been researching what Mocking framework to use that will allow me to mock SharePoint classes, and as a result I am evaluating Telerik JustMock.

I am using SharePoint 2007 and Visual Studio 2008

I am attemting to Mock a SPList and a SPListItemCollection (see test method below) and I am getting the following error
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->  
System.MethodAccessException: Microsoft.SharePoint.SPListItemCollection..ctor().
"

 when attempting to execute the bold line below;

        [TestMethod]
        public void Should_Assert_FakingOfSpList()
        {
            var spWeb = Mock.Create<SPWeb>();
            var spList = Mock.Create<SPList>();
            var spListCollection = Mock.Create<SPListCollection>();
            var spListItemCollection = Mock.Create<SPListItemCollection>();
            Mock.Arrange(() => spWeb.Lists).Returns(spListCollection);
            Mock.Arrange(() => spListCollection[Arg.AnyString]).Returns(spList);
            Mock.Arrange(() => spList.GetItems(Arg.IsAny<SPQuery>())).Returns(spListItemCollection);
            Assert.AreEqual(spListCollection, spWeb.Lists);
            Assert.AreEqual(spList, spWeb.Lists["myList"]);
            Assert.AreEqual(spListItemCollection, spWeb.Lists["myList"].GetItems(new SPQuery()));
        }
    }

I was wondering if the error was in the trial version and not in the full blown version but as you can imagine, I am not keen to purchase JustMock if I am going to come across the same issue.

I would appreciate any help on this matter.

Many thanks

Gary Hoggart
Senior IT Specialist



Ricky
Telerik team
 answered on 24 Feb 2011
5 answers
274 views
It would really be helpful if you put up a few examples of using JustMock against SharePoint. Anyways, I am trying to Mock a call to the GetItems method of the SPList object but I keep getting a NullReference error. Any help would be appreciated. 

Here is the code I am attempting to mock:

var collection = web.Lists[listName].GetItems(query); 

 

 

 

Here is my JustMock code

 

 

 

private SPWeb mockSPWeb = Mock.Create<SPWeb>(); 
private SPList mockSPList = Mock.Create<SPList>(); 
private SPListCollection mockSPListCollection = Mock.Create<SPListCollection>(); 
private SPListItemCollection mockSPListItemCollection = Mock.Create<SPListItemCollection>(); 
  
Mock.Arrange(() => mockSPWeb.Lists).Returns(mockSPListCollection); 
Mock.Arrange(() => mockSPListCollection[Arg.IsAny<string>()]).Returns(mockSPList); 
Mock.Arrange(() => mockSPList.GetItems(new SPQuery())).Returns(mockSPListItemCollection); 
  
var testEntityRepository = new TestEntityRepository(new ListItemRepository()); 
var entity = testEntityRepository.GetASingleTestEntity(mockSPWeb);

I've tried several different ways to Mock this all with the same error:

Test method HealthDialog.SharePoint.Foundation.UnitTests.BaseEntityTests.TestBaseEntityRepository threw exception:  System.NullReferenceException: Object reference not set to an instance of an object..

 

 

Ricky
Telerik team
 answered on 11 Feb 2011
1 answer
138 views
I am sure I am just blind but I can't find license info for installing on TFS 2010 for automated builds. What covers this license, is a separate license needed for the server? Where do I find that license information?
Ricky
Telerik team
 answered on 09 Feb 2011
5 answers
2.7K+ views
I am mocking a web-service method in my test using JustMock. The mock works as expected when I run it in debug mode. However when I run it not in debug mode, the mock fails to intercept the call and hits the actual web-service method. I'm using *Any* args in my arrange as I remember seeing something about a bug that using those fixed... 
Any ideas? Looks like a bug to me.

VS2010 Premium
JustMock (2010.3.1109.7 paid) is ON
Running from VS IDE using built-in MSTest and runner.

-Jason
Ricky
Telerik team
 answered on 26 Jan 2011
3 answers
220 views
I have been experiencing anomalous behavior with VS 2010 Ultimate SP1 beta crashing every time I close the IDE, and then it repeatedly restarts and spawns a new devenv process and IDE.

According to Microsoft Connect this is a known bug in VS 2010 but they have not yet released a workaround, patch, and won't say anything other than it will be available with the final RTM of SP1.

However, there are some notes to suggest that the problem may be related to add-in and extension menus that have been added to the IDE. So in an effort to investigate, I have attempted to remove various extensions and control sets.

I have been unable to remove the JustMock extensions from the Extension Manager. It says to remove via Windows Add/Remove Control Panel which I did successfully. So JustMock has been UNinstalled from Windows Control Panel and is no longer visible there. But it remains in VS 2010 Extension Manager.

So how to remove the JustMock extension?

I do wish to continue using VS 2010 Ultimate SP1 beta. And I am not yet willing to completely UNinstall VS 2010, and go through the process of re-installing it.
Chris
Telerik team
 answered on 25 Jan 2011
2 answers
109 views
I've got a bunch of tests that need to mock SPAttachmentCollection. In Isolator, I did it like this:


public void CreateAttachmentFromAfterUrl_AttachmentNameNotInAfterUrl_ReturnsNull()
{
    var properties = Isolate.Fake.Instance<SPItemEventProperties>();
 
    Isolate.WhenCalled(() => properties.ListItem.Attachments)
        .WillReturnCollectionValuesOf(new[] { "A","B" });
 
    Isolate.WhenCalled(() => properties.AfterUrl)
        .WillReturn("C");
 
    var attachment = AttachmentFactory.CreateAttachmentFromAfterUrl(properties);
 
    attachment.ShouldBeNull();
}

In JustMock, I've done it like this:

public void CreateAttachmentFromAfterUrl_AttachmentNameNotInAfterUrl_ReturnsNull()
{
    var properties = Mock.Create<SPItemEventProperties>();
    var collection = Mock.Create<SPAttachmentCollection>();
 
    var list = new List<string> {"A", "B"};
 
    Mock.Arrange(() => collection.GetEnumerator()).Returns(list.GetEnumerator());
    Mock.Arrange(() => properties.ListItem.Attachments).Returns(collection);
    Mock.Arrange(() => properties.AfterUrl).Returns("C");
 
    var attachment = AttachmentFactory.CreateAttachmentFromAfterUrl(properties);
 
    attachment.ShouldBeNull();
}

Is there a better way? I would like to use ReturnsCollection() on properties.ListItem.Attachments, but it's unavailable for some reason.

-Andy
Ricky
Telerik team
 answered on 24 Jan 2011
3 answers
287 views
I'm trying to test some web aware code using HttpContext.Current (Request & Server).

JustMock is supposed to support this, but the working example is very simple and not what I want to achieve.

What I Want to mock is

if (HttpContext.Current != null)
{
string s = HttpContext.Current.Request....
string t = HttpContext.Current.Server....
}

Can Justmock do this very simple task (I'm using the professional version)?.

Thanks

Ian
Ricky
Telerik team
 answered on 14 Jan 2011
1 answer
90 views

Hi,

I want to create a unit test for the CheckUser method presented below...  In that method we create a new instance of a class named LoginService.  Inside my unit test, I know how to arrange my LoginService instance so that the mocking framework will replace the original call to ValidateUser with a simple "Return 1".  That is not my problem....  My problem is that the parameterless constructor of the LoginService class used in CheckUser does a couple things I'd prefer not to do in my unit test like (as an example) connect to an Active Directory.  Of course, if my unit test tries systematically to create a REAL LoginService then it will always fail as I have no active directory ready for my tests...  Is there a way with JustMock to tell the mocking infrastructure that whenever my code tries to instanciate a class of type LoginService then I want a MOCK (or proxy) of LoginService to be created instead?  On this mock I would of course have set arrange statements to have it behave like I want, let's say, Return 1 when ValidateUser is invoked on the Mock (or proxy)...

public class LegacyCode
{
    
public int CheckUser(string userName, string password)
    {
      ---->  
LoginService _service = new LoginService();
        
        [some code ommited here]
        
        return _service.ValidateUser(userName, password);
    }
}

I know some other mocking products out there support such a feature and I want to know if JustMock supports it also?

Thanks for your time.

Ricky
Telerik team
 answered on 13 Jan 2011
3 answers
117 views
I'm still trying to figure out how this all works (on the fence on Mocking right now)

Can someone explain what's going on here?
// Arrange
var filename = this.GetType().Assembly.ManifestModule.FullyQualifiedName;
var fi = new FileInfo(filename);
 
bool called = false;
 
Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
 
// Act
fi.Delete();
 
// Assert
Assert.IsTrue(called);

In plain english, what is this line doing
Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
Is it saying that when .Delete is called on the fi object, dont do the default, instead just return true or something?

What is the () => lambda syntax mean?
Ricky
Telerik team
 answered on 11 Jan 2011
8 answers
166 views
Hi,

Yesterday, I tried to mock a property of an object to define the return value but it doesn't work. I know that I am two levels depth but can you explain me what is wrong with this code...

Public Interface IProcessor
   Property Connection As IConnection
   Function Execute() As Boolean
End Interface
 
Public Interface IConnection
   Function Open() As Boolean
End Interface
 
Public Class DatabaseExecutor
   Implements IProcessor
 
   Public Property Connection As IConnection Implements IProcessor.Connection
 
   Public Function Execute() As Boolean Implements IProcessor.Execute
      If Me.Connection.Open Then
         Return True
      Else
         Return False
      End If
   End Function
End Class
 
Imports NUnit.Framework
Imports Telerik.JustMock
 
<TestFixture()>
Public Class DatabaseExecutorTests
   ''' <summary>
   ''' This test fail because I try to mock the function open of the objet IConnection directly
   ''' from the property Connection of the object Processor.
   ''' </summary>
   ''' <remarks></remarks>
   <Test()>
   Public Sub Execute_SimpleRequest_ReturnsTrue()
      Dim executor As New DatabaseExecutor
 
      executor.Connection = Mock.Create(Of IConnection)()
 
      Mock.Arrange(Function() executor.Connection.Open).Returns(True)
 
      Assert.AreEqual(True, executor.Execute)
   End Sub
 
   ''' <summary>
   ''' This test pass because I create the connection separatly of the Processor objet and then
   ''' assign it to the property Processor.
   ''' </summary>
   ''' <remarks></remarks>
   <Test()>
   Public Sub Execute_SimpleRequestViaProperty_ReturnsTrue()
      Dim executor As New DatabaseExecutor
      Dim connection As IConnection = Mock.Create(Of IConnection)()
 
      Mock.Arrange(Function() connection.Open).Returns(True)
 
      executor.Connection = connection
 
      Assert.AreEqual(True, executor.Execute)
   End Sub
End Class

If you want, I can send you a demo but I think this will be very easy question for you.
Ricky
Telerik team
 answered on 07 Jan 2011
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?