or
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
using Telerik.JustMock; |
namespace MockTest |
{ |
class Program |
{ |
static void Main(string[] args) |
{ |
ICustomer cust = Mock.Create<ICustomer>(); |
Order ord = new Order(cust); |
} |
} |
} |
SPContentTypeCollection mockedContentTypeCollection = Mock.Create<SPContentTypeCollection>();
Mock.Arrange(() => mockedList.ContentTypes).Returns(mockedContentTypeCollection);
SPContentTypeId mockedContentTypeId = Mock.Create<SPContentTypeId>();
Mock.Arrange(() => mockedContentTypeCollection.BestMatch(Arg.IsAny<SPContentTypeId>())).Returns(mockedContentTypeId).MustBeCalled();
Test method XX.XX.Data.Tests.Repositories.XXRepositoryTests.AddPaymentSucceedsTest threw exception:
System.NullReferenceException: Object reference not
set
to an instance of an
object
.
at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.GetRuntimeMethod(IInvocation invocation)
at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.Telerik.JustMock.Weaver.Interceptors.Abstraction.IWeaverInterceptor.Intercept(IInvocation invocation)
at SPContentTypeId_Interceptor_d2e70383884d4efca5b3ddb76451ab0c.Intercept(Object, SPContentTypeId,
ref
Boolean)
at Microsoft.SharePoint.SPContentTypeId.Equals(Object o)
at Telerik.JustMock.Utility.IsDefault(Type valueType, Object value)
at Telerik.JustMock.Setup.MethodCall.GetNewInstance(Object[] arguments, Type[] argumentTypes)
at Telerik.JustMock.Interceptors.MockInterceptor.CreateMethodInstance(Object[] userArgs, Type[] types)
at Telerik.JustMock.Interceptors.MockInterceptor.CreateMethodInstance(Object[] userArgs, Type[] types, Instruction instruction)
at Telerik.JustMock.Interceptors.MockInterceptor.Intercept(MockInvocation invocation)
at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.Telerik.JustMock.Weaver.Interceptors.Abstraction.IWeaverInterceptor.Intercept(IInvocation invocation)
at Telerik.JustMock.Handlers.WeaverInterceptorHandler.Invoke(Object[] args)
at Telerik.JustMock.MockContext`1.SetupMock(MockExpression`1 expression)
at Telerik.JustMock.MockContext`1.SetupMock(Expression`1 expression)
at Telerik.JustMock.Mock.<>c__DisplayClass1`1.<Arrange>b__0(MockContext`1 x)
at Telerik.JustMock.MockContext.Setup(Instruction instruction, Func`2 function)
at Telerik.JustMock.Mock.Arrange(Expression`1 expression)
at XX.XX.Data.Tests.Repositories.XXRepositoryTests.AddPaymentSucceedsTest()
in
XXRepositoryTests.cs: line 163
Test(s) failed. Telerik.JustMock.MockException : Could not create mock from sealed class when profiler is not enabled.
at Telerik.JustMock.MockManager.CreateInstance(Container container)
at Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static)
at Telerik.JustMock.MockManager.CreateInstance()
at Telerik.JustMock.Mock.Create(Type target, Behavior behavior, Object[] args)
at Telerik.JustMock.Mock.Create(Type target, Object[] args)
at Telerik.JustMock.Mock.Create[T]()
var httpResponse =
new
HttpResponse(
null
);
var httpRequest =
new
HttpRequest(
string
.Empty,
"http://somewhere.com/"
,
null
);
var currentContext = Mock.Create<HttpContext>(() =>
new
HttpContext(httpRequest, httpResponse));
Mock.Arrange(() => HttpContext.Current).Returns(currentContext);
Assert.IsNotNull(HttpContext.Current);
Assert.IsNotNull(HttpContext.Current.Request);<br>
[TestMethod]
public
void
TfsTeamProjectCollection_Create_returns_VersionControlServer()
{
// Arrange
var tfsTeamProjectCollection = Mock.Create<TfsTeamProjectCollection>(Constructor.Mocked);
var versionControlServer = Mock.Create<VersionControlServer>();
Mock.Arrange(() => tfsTeamProjectCollection.GetService<VersionControlServer>())
.Returns(versionControlServer)
.IgnoreInstance();
var newTfsTeamProjectCollection =
new
TfsTeamProjectCollection(
new
Uri(
"about:blank"
));
// Act
var result = newTfsTeamProjectCollection.GetService<VersionControlServer>();
// Assert
Assert.AreEqual(versionControlServer, result);
}
[TestMethod]
public
void
TfsTeamProjectCollection_Create_throws_NotImplementedException()
{
// Arrange
var tfsTeamProjectCollection = Mock.Create<TfsTeamProjectCollection>(Constructor.Mocked);
var versionControlServer = Mock.Create<VersionControlServer>();
Mock.Arrange(() => tfsTeamProjectCollection.GetService<VersionControlServer>())
.Throws(
new
NotImplementedException())
.IgnoreInstance();
var newTfsTeamProjectCollection =
new
TfsTeamProjectCollection(
new
Uri(
"about:blank"
));
try
{
// Act
newTfsTeamProjectCollection.GetService<VersionControlServer>();
// Assert
Assert.Fail(
"An Exception was expected."
);
}
catch
(NotImplementedException)
{
}
[TestClass] public class MobileAppSectionServiceTests { private const int AppId = 1; private const int LocalCultureId = 2; private IEnumerable<IMobileAppSection> sections; private GetSectionsForAppRequest request; private MobileAppSection section; [TestInitialize] public void TestInit() { section = new MobileAppSection(); section.Id = AppId; section.Name = "SectionName"; section.Summary = "Section Summary"; sections = new List<IMobileAppSection> { section }; request = new GetSectionsForAppRequest { MobileAppId = AppId, LocalCultureId = LocalCultureId}; } [TestMethod] public void GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse() { MobileAppSectionService service = new MobileAppSectionService(null); Mock.Arrange<IEnumerable<IMobileAppSection>>(() => service.GetSectionDomainEntitiesForAppByCulture(AppId, LocalCultureId)).Returns(sections); GetSectionsForAppResponse response = service.GetSectionsForAppByCulture(request); Assert.IsNotNull(response, "Response is not null"); } [TestMethod] public void GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsResponseWithSections() { IMobileAppSectionMapper _mobileAppSectionMapper = Mock.Create<IMobileAppSectionMapper>(); MobileAppSectionService service = new MobileAppSectionService(_mobileAppSectionMapper); Mock.Arrange<IEnumerable<IMobileAppSection>>(() => _mobileAppSectionMapper.GetSectionsForMobileApp(AppId, LocalCultureId)).Returns(sections); GetSectionsForAppResponse response = service.GetSectionsForAppByCulture(request); Assert.IsTrue(response.MobileAppSections.Any()); } } The error message is: Test method MobileAppDelivery.Service.Tests.Unit.MobileAppSectionServiceTests.GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse threw exception:
System.NullReferenceException: Object reference not set to an instance of an object. The NullReferenceException is thrown the following method of the MobileAppSectionService class.public List<IMobileAppSection> GetSectionDomainEntitiesForAppByCulture(int appId, int cultureId) { List<IMobileAppSection> sections = _mobileAppSectionMapper.GetSectionsForMobileApp(appId, cultureId); return sections; } where _mobileAppSectionMapper is null. Rewriting the test method as the following fixes the problem[TestMethod] public void GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse() { IMobileAppSectionMapper mapper = Mock.Create<IMobileAppSectionMapper>(); MobileAppSectionService service = new MobileAppSectionService(mapper); Mock.Arrange<IEnumerable<IMobileAppSection>>(() => service.GetSectionDomainEntitiesForAppByCulture(AppId, LocalCultureId)).DoNothing().Returns(sections); GetSectionsForAppResponse response = service.GetSectionsForAppByCulture(request); Assert.IsNotNull(response, "Response is not null"); } So why does the original test only pass when run individually? My instincts tell me that the original test was not written correctly but it still passed. Thanks!