Search code examples
c#unit-testingnservicebusnservicebus5

NServiceBus Test.Initialize() given key was not present


Update just read the Google group in more detail and apparently it's a app config issue. Will update post after trying.

I was wondering if anyone knows of this issue when attempting to unit test NServiceBus.

Realise I have two initialize methods below but this is to illustrate what I am trying to do. Stack trace based on request.

(NServiceBus.LocalAddress) was not present in the dictionary. (NServiceBus.LocalAddress) was not present in the dictionary.

Other part of the stack trace I believe is a red herring complaining about InMemoryPersistence. However, there is a Google group talking about this issue too who experienced the same issue which makes me think it's more of an NServiceBus issue rather than a coding mistake.

Google group link https://groups.google.com/forum/m/#!topic/particularsoftware/424_6KCv6oI

Should mention seen these posts.

https://github.com/Particular/NServiceBus.Testing/issues/20 https://github.com/Particular/NServiceBus.Testing/commit/f761c5391b03b05d967f2e368248c72522051d59

public static class CustomInit
    {
        public static void Init()
        {
            //Previous versions of NBUS, specifically 4.6.5
            //MessageConventionExtensions.IsEventTypeAction = MessageConfiguration.ForEvents();
            //MessageConventionExtensions.IsCommandTypeAction = MessageConfiguration.ForCommands();            

            //New 5.2.0 how to setup tests
            Test.Initialize(x => x.AssembliesToScan(GetAssembliesToScan()));

            Test.Initialize(
                x =>
                {
                    x.Conventions().DefiningCommandsAs([my namespace]);
                    x.Conventions().DefiningEventsAs([my namespace]);
                    x.AssembliesToScan(GetAssembliesToScan());
                });
        }

        private static IEnumerable<Assembly> GetAssembliesToScan()
        {
            return new[]
            {
                AssemblyFromType<ISomeInterface>()
            };
        }
}

Solution

  • After raising the issue on GitHub found that need to include NServiceBus.Testing as part of the assembly scanning. For example:

    Should point out also, for further information I would visit the GitHub link. Further detail about the issue and an explanation can be found there.

        public static void Init()
        {
            Test.Initialize(
                x =>
                {
                    x.Conventions().DefiningCommandsAs(x => x.Namespace.Contains("Commands"));
                    x.Conventions().DefiningEventsAs(x => x.Namespace.Contains("Events"));
                    x.AssembliesToScan(GetAssembliesToScan());
                });
        }
    
        private static IEnumerable<Assembly> GetAssembliesToScan()
        {
            return new[]
            {
                AssemblyFromType<ISomeInterface>(),
                Assembly.LoadFrom("NServiceBus.Testing.dll")
            };
        }
    

    The key point being this Assembly.LoadFrom("NServiceBus.Testing.dll")

    Cheers, DS.