Trouble unit testing services.....
See original GitHub issueHi Halil,
I have read the post at http://www.codeproject.com/Articles/871786/Unit-testing-in-Csharp-using-xUnit-Entity-Framewor and am now trying to set up some unit tests. However I am having some issues with the IocResolver.
So I created my base unit test class:
public abstract class AbpTestsBase : AbpIntegratedTestBase
{
protected override void AddModules(ITypeList<AbpModule> aModules)
{
base.AddModules(aModules);
//Adding testing modules. Dependant modules are automatically added.
aModules.Add<OzCruisingDesktopApplicationModule>();
//Expressly *force* the platform application module to be loaded just to rule out the module dependency subsystem
aModules.Add<OzCruisingPlatformApplicationModule>();
}
}
and then my unit test class (unsing nUnit) looks like:
[TestFixture]
public class QuickFindServiceTests : AbpTestsBase
{
private IQuickFindService _QuickFindService;
[Test]
public void GetDepartFromFirstItemIsAllAustralianPorts()
{
//Arrange
GetDepartFromsInput param = new GetDepartFromsInput
{
IncludeAllAustralianPorts = true
};
//Act
GetDepartFromsOutput result = _QuickFindService.GetDepartFroms(param);
//Assert
result.DepartFroms.Count.ShouldBeGreaterThanOrEqualTo(1);
result.DepartFroms.First().Name.ShouldBe("All Australian Ports");
}
/// <summary>
/// Called at the commencement of all the unit tests.
/// </summary>
[TestFixtureSetUp]
public void Init()
{
//_QuickFindService = IocManager.Instance.Resolve<IQuickFindService>();
_QuickFindService = LocalIocManager.Resolve<IQuickFindService>();
}
}
So using the LocalIocManager.Resolve<>() means I find my QuickFindService all right. However this ultimately calls:
public class QuickFindService : OzCruisingDesktopAppServiceBase, IQuickFindService
{
/// <summary>
/// Gets the departure ports to display in the left hand side navigation quick find.
/// </summary>
/// <param name="aParams"></param>
/// <returns>Returns the departure ports to display in the left hand navigation, in ascending ship name order.</returns>
public GetDepartFromsOutput GetDepartFroms(GetDepartFromsInput aParams)
{
//Default behaviour
GetDepartFromsOutput result = new GetDepartFromsOutput();
IOzCpReferenceDepartingFromListService platServiceDepartingFroms = IocManager.Instance.Resolve<IOzCpReferenceDepartingFromListService>();
OzCpGetAllDepartingFromsOutput platformDepartingFroms = platServiceDepartingFroms.GetAllDepartingFroms(new OzCpGetAllDepartingFromsInput
{
SortDirection = SimpleOrderingDirectionEnum.Ascending,
SortField = SimpleOrderingFieldEnum.ByText
});
...
return result;
}
}
Note specifically the call to IocManager.Instance.Resolve<IOzCpReferenceDepartingFromListService>(); which is using the singleton of the IocManager to get the references it needs. Clearly this singleton has not been initialised as I get:
So my questions are:
1). What calls am I missing in my Unit Test project to “seed” the IocManager 2) Assuming I have done 1) there should not be any need to use the property LocalIocManager anymore
I am guessing I should rather use an instance of AbpBootstrapper() perhaps? But then there is no “easy and obvious” way to register modules as the class AbpIntegratedTestBase provides.
–D
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
Hi,
There are some problems here;
Please fix these and try again.
Hi,
Thanks for the clarification. Instead of modifying the class I created an adapter which I am using, which ends up a better solution I think as you can now extend AbpIntegratedTestBase without breaking my code. I include it here in case it is useful to someone else who is using nUnit: