Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

System.NullReferenceException inside TechTalk.SpecFlow #24

Closed
304NotModified opened this issue Feb 17, 2020 · 6 comments · Fixed by #25
Closed

System.NullReferenceException inside TechTalk.SpecFlow #24

304NotModified opened this issue Feb 17, 2020 · 6 comments · Fixed by #25
Assignees

Comments

@304NotModified
Copy link
Contributor

304NotModified commented Feb 17, 2020

Hi,

I added this to my specflow project:

    <PackageReference Include="SpecFlow" Version="3.1.80" />
    <PackageReference Include="SpecFlow.MsTest" Version="3.1.80" />
    <PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.1.80" />
    <PackageReference Include="SolidToken.SpecFlow.DependencyInjection" Version="0.3.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.0.0" />

and

	public class SpecflowHooks
	{
		
		[ScenarioDependencies]
		public static IServiceCollection CreateServices()
		{
			var services = new ServiceCollection();
			return services;
		}
	}

And now I get for all tests:

System.NullReferenceException: Object reference not set to an instance of an object.
    at lambda_method(Closure , IContextManager , String )
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration) in D:\a\1\s\TechTalk.SpecFlow\Bindings\BindingInvoker.cs:line 69
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 501
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 422
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.Step(StepDefinitionKeyword stepDefinitionKeyword, String keyword, String text, String multilineTextArg, Table tableArg) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 581
   at TechTalk.SpecFlow.TestRunner.Given(String text, String multilineTextArg, Table tableArg, String keyword) in D:\a\1\s\TechTalk.SpecFlow\TestRunner.cs:line 80
   at 

any idea?

Update: same issue with 0.3.1 and 0.2.4. What am I missing?

@304NotModified 304NotModified changed the title System.NullReferenceException System.NullReferenceException inside TechTalk.SpecFlow Feb 17, 2020
@mbhoek
Copy link
Member

mbhoek commented Feb 17, 2020

Let me investigate and I will get back to you.

@mbhoek mbhoek self-assigned this Feb 17, 2020
@304NotModified
Copy link
Contributor Author

FYI:

 <TargetFramework>netcoreapp2.1</TargetFramework>

tried also static class, but that won't help either.

The big difference is here (I think), that we're using MSTest and not xUnit

@304NotModified
Copy link
Contributor Author

I found the issue (I have checked out the repo)

If the dependency isn't found, you will get this nasty exception.

So:

namespace SolidToken.SpecFlow.DependencyInjection.Tests
{
    public static class TestDependencies
    {
        [ScenarioDependencies]
        public static IServiceCollection CreateServices()
        {
            var services = new ServiceCollection();

            //// Add test dependencies
            //services.AddTransient<ITestService, TestService>();

            //// ContextInjectionScope (by using AddScoped instead of AddTransient, the context will be scoped to the Feature across bindings)
            //services.AddScoped<TestContext>();

            //// NOTE: This line is essential so that Microsoft.Extensions.DependencyInjection knows
            //// about the SpecFlow bindings (something normally BoDi does automatically).
            //// TODO: Find out if we can make this part of the Plugin
            //foreach (var type in typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
            //{
            //    services.AddSingleton(type);
            //}

            return services;
        }
    }
}

Will demo the issue

@mbhoek
Copy link
Member

mbhoek commented Feb 17, 2020

Is it possible that you have not registered any bindings (yet)?

For example, if you look at DependencyInjectionPluginSteps.cs you will notice the [Binding] tag that will bind the code to the SpecFlow Feature file.

Currently the plugin requires you to register all the bindings/step definitions (otherwise the DI framework can't find them). So your initialisation code should look like:

  var services = new ServiceCollection();

  foreach (var type in typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
  {
      services.AddSingleton(type);
  }

  return services;

Although this is in the README.md, I don't think it is very intuitive and I want to solve it as part of issue #7 but haven't landed on a solution yet.

@304NotModified
Copy link
Contributor Author

ow yes indeed. I missed that! That's the fix.

Would be great if we could make that optional.

@304NotModified
Copy link
Contributor Author

@mbhoek check #25 :)

This could be closed.

mbhoek added a commit that referenced this issue Feb 17, 2020
Initial thought was that issue #24 was cause by using the MSTest testing framework. Although this was not the case, it does make sense to add tests based on MSTest testing framework.

Still needs cleanup (rename the existing XUnit test project), support for both 2.1 and 3.1 SDK and investigate whether it is possible to code share between MSTest and XUnit test project.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants