Hi,
I am having a problem with JustMock where tests pass when run individually but fail when all tests are run. I am not using any static or final mocking.
In the following code, the GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse test fails.
I am having a problem with JustMock where tests pass when run individually but fail when all tests are run. I am not using any static or final mocking.
In the following code, the GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse test fails.
[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!