or
[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!
ReturnsCollection
[TestClass] public class SPM150ControllerTestBase { public IAssetsService AssetsServiceMock { get; set; } public INavigationService NavigationServiceMock { get; set; } public ISPM150Service SPM150ServiceMock { get; set; } public IGlobalData GlobalDataMock { get; set; } public SPM150Controller Target { get; set; } [TestInitialize] public void TestInit() { AssetsServiceMock = Mock.Create<IAssetsService>(); SPM150ServiceMock = Mock.Create<ISPM150Service>(); NavigationServiceMock = Mock.Create<INavigationService>(); GlobalDataMock = Mock.Create<IGlobalData>(); Target = Mock.Create<SPM150Controller>(Behavior.CallOriginal, new object[] { AssetsServiceMock, SPM150ServiceMock, NavigationServiceMock, GlobalDataMock, null }); OnInitialize(); } protected virtual void OnInitialize() { } }[TestClass] public class UpdateEquipmentTest : SPM150ControllerTestBase { public IEquipmentListItem UpdateItemMock { get; set; } public IEquipmentListItem ExistingItemMock { get; set; } //[TestInitialize] //public void OnInit() //{ // base.TestInit(); //} protected override void OnInitialize() { UpdateItemMock = Mock.Create<IEquipmentListItem>(); ExistingItemMock = Mock.Create<IEquipmentListItem>(); //Mock.Arrange(() => UpdateItemMock.BenchmarkValue).Returns(100); //Mock.Arrange(() => UpdateItemMock.Classification).Returns("No Class"); //Mock.Arrange(() => UpdateItemMock.DeviceName).Returns("A Device"); //Mock.Arrange(() => UpdateItemMock.IPAddress).Returns("000.000.000.000"); //Mock.Arrange(() => UpdateItemMock.Request).Returns(0); //Mock.Arrange(() => ExistingItemMock.BenchmarkValue).Returns(100); //Mock.Arrange(() => ExistingItemMock.Classification).Returns("No Class"); //Mock.Arrange(() => ExistingItemMock.DeviceName).Returns("A Device"); //Mock.Arrange(() => ExistingItemMock.IPAddress).Returns("000.000.000.000"); //Mock.Arrange(() => ExistingItemMock.Request).Returns(0); UpdateItemMock.BenchmarkValue = 100.0; UpdateItemMock.Classification = "No Class"; UpdateItemMock.DeviceName = "A Device"; UpdateItemMock.IPAddress = "000.000.000.000"; UpdateItemMock.ModbusUnitId = 100; UpdateItemMock.Request = 0; //set with default values can change if needed... ExistingItemMock.BenchmarkValue = 100.0; ExistingItemMock.DeviceName = "A Device"; ExistingItemMock.ModbusUnitId = 100; ExistingItemMock.IPAddress = "000.000.000.000"; ExistingItemMock.Request = 0; Mock.Arrange(() => Target.LogBenchmarkValueChange(Arg.AnyString, Arg.AnyDouble, Arg.AnyString, Arg.AnyString, Arg.AnyDouble)).DoNothing(); Mock.Arrange(() => Target.LogIPChange(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString)).DoNothing(); Mock.Arrange(() => Target.LogModBusUnitIdChange(Arg.AnyString, Arg.AnyInt, Arg.AnyString, Arg.AnyString, Arg.AnyInt)).DoNothing(); Mock.Arrange(() => Target.LogDeviceNameChange(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString)).DoNothing(); } [TestMethod] public void Test0020CheckForSiteAndUserCalledCorrectParametersVersion1() { Mock.Arrange(() => Target.CheckForSiteNumberAndUserName(Arg.AnyString, Arg.AnyString)).DoNothing().OccursOnce(); Mock.Arrange(() => SPM150ServiceMock.GetEquipmentDetails(Arg.AnyInt)).Returns(ExistingItemMock); Target.UpdateEquipment("12345", UpdateItemMock, "Hi Bob!"); //Target.Assert(); //Target.Assert(x => x.CheckForSiteNumberAndUserName("12345", "Hi Bob!")); Mock.Assert(() => Target.CheckForSiteNumberAndUserName("12345", "Hi Bob!")); }}Test method SPM150.Test.LogBenchmarkValueChangeTest.Test0020LogTransactionCalledWhenValuesDifferentCorrectParmetersVersion1 threw exception: Telerik.JustMock.MockException: Profiler must be enabled to mock/assert target SPM150ControllerTestBase.get_AssetsServiceMock() method.at Telerik.JustMock.Handlers.InterceptorHandler.Create(Object target, MethodInfo methodInfo, Boolean privateMethod)at Telerik.JustMock.MockContext`1.SetupMock(MockExpression`1 expression)at Telerik.JustMock.Mock.<>c__DisplayClass13.<Assert>b__12(MockContext`1 x)at Telerik.JustMock.MockContext.Setup(Instruction instruction, Action`1 action)at Telerik.JustMock.Mock.Assert(Expression`1 expression, Args args, Occurs occurs)at Telerik.JustMock.Mock.Assert(Expression`1 expression)at SPM150.Test.LogBenchmarkValueChangeTest.Test0020LogTransactionCalledWhenValuesDifferentCorrectParmetersVersion1() in SPM150ControllerTest.cs: line 545public abstract class AbstractStateMachine{ public void DoClose() { //do something close(); } public abstract void close();}[TestMethod]public void TestMethod1(){ AbstractStateMachine sm = Mock.Create<AbstractStateMachine>(Behavior.CallOriginal); Mock.Arrange(() => sm.close()).DoNothing().MustBeCalled(); sm.DoClose();}public class MyClass{ public long AddOne(long number) { return number + 1; }}[TestMethod]public void MyClass_AddOne_mock_test_int(){ // Arrange int number = 5; int expectedResult = 42; var myClass = Mock.Create<MyClass>(); Mock.Arrange(() => myClass.AddOne(number)) .Returns(expectedResult); // Act var result = myClass.AddOne(number); // Assert Assert.AreEqual(expectedResult, result); // result = 0
}Mock.Assert(() => myClass.AddOne(number), Occurs.Once());[TestMethod]public void Assert_int_vs_long(){ int intNumber = 5; long longNumber = 5; Assert.AreEqual(intNumber, longNumber);}public static bool DirectoryExists(string directory){ return DirectoryExists(new DirectoryInfo(directory));}private static bool DirectoryExists(DirectoryInfo directory){ bool value = Directory.Exists(directory.FullName); return value;}