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..
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();
}
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();
}
if
(HttpContext.Current !=
null
)
{
string
s = HttpContext.Current.Request....
string
t = HttpContext.Current.Server....
}
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.
// 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);
Mock.Arrange(() => fi.Delete()).DoInstead(() => called =
true
);
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