question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble unit testing services.....

See original GitHub issue

Hi 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:

image

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:closed
  • Created 8 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
hikalkancommented, Aug 27, 2015

Hi,

There are some problems here;

  1. I strongly suggest to use xUnit rather than nUnit. It will be easy to change.
  2. In unit test, never use IocManager.Instance. It’s singleton and if you use it, your tests effect each other.
  3. If you need to IocManager, use LocalIocManager always. Because it’s created per test.

Please fix these and try again.

0reactions
natikicommented, Sep 1, 2015

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:

    /// <summary>
    ///     Adapter class so that we can more easily use the ABP test class with
    ///     nUnit. AbpIntegratedTestBase is written for xUnit which assumes that
    ///     we will use the constructor for each test run and hence override the ModulesAdd()
    ///     method. However in nUnit with the test class only instantiated once we need
    ///     to do this via the adapter.
    /// </summary>
    /// <remarks>
    ///     To use this class:
    ///     - Create a descendant and override AddModules() adding the APB modules needed
    ///     - Create an instance of this class in nUnit Setup() method
    ///     - Destroy instance of this class in nUnit TearDown() method
    /// </remarks>
    public abstract class OzAbpTestsAdapterBase : AbpIntegratedTestBase
    {
        public new IIocManager LocalIocManager
        {
            get { return base.LocalIocManager; }
        }
    }


   /// <summary>
    ///     Adapter class to work with the ABP base test class, to ensure the IocManager is correctly
    ///     instantiated.
    /// </summary>
    public class AbpApplicationServiceTestsAdapter : OzAbpTestsAdapterBase
    {
        protected override void AddModules(ITypeList<AbpModule> aModules)
        {
            base.AddModules(aModules);

            //Add testing modules. Dependant modules are automatically added.
            aModules.Add<MyDesktopApplicationModule>();
            aModules.Add<MyPlatformApplicationModule>();
            aModules.Add<MyPlatformDataModule>();
        }
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing services
To test a service, you set the providers metadata property with an array of the services that you'll test or mock. app/demo/demo.testbed.spec.ts (provide ......
Read more >
Testing Services – Testing Angular
All in all, testing Services is easier than testing other Angular application parts. Most Services have a clear purpose and a well-defined ...
Read more >
Testing Angular Services: A Walk-Through With Examples
Learn how to take advantage of built-in paths for testing Angular services which can help you deliver high-quality code consistency.
Read more >
Angular unit testing tutorial with examples
An Angular unit test aims to uncover issues such as incorrect logic, misbehaving functions, etc. by isolating pieces of code. This is sometimes ......
Read more >
Angular Unit Testing Services - YouTube
My easiest approach to Angular unit testing services. ... to view courses where we show the problem, then we try to solve it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found