aspnet/Testing

System.InvalidOperationException: Object reference not set to an instance of an object.

scottengle opened this issue · 4 comments

I am getting the following error message when building my test project:

------ Discover test started ------
------ Test started: Project: AdminTools.Tests ------
Starting Microsoft.Framework.TestHost [C:\Users\sengle\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta6-11941\bin\dnx.exe --appbase "C:\projects\AdminTools\test\AdminTools.Tests" Microsoft.Framework.ApplicationHost --port 21564 Microsoft.Framework.TestHost --port 41925 list ]
Connected to Microsoft.Framework.TestHost
Discovering tests in 'C:\projects\AdminTools\test\AdminTools.Tests\project.json'
Error discovering tests in 'C:\projects\AdminTools\test\AdminTools.Tests\project.json'
System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Object reference not set to an instance of an object.
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Web.UnitTest.TestAdapter.Discovery.HostExtensions.GetTests(IHost host, ITestCaseDiscoverySink discovery, IMessageLogger logger, KRuntimeConfiguration testConfiguration)
   at Microsoft.VisualStudio.Web.UnitTest.TestAdapter.KUnitTestDiscoverer.GetTestCases(IEnumerable`1 sources, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
---> (Inner Exception #0) System.InvalidOperationException: Object reference not set to an instance of an object.<---

========== Discover test finished: 0 found (0:00:02.3672367) ==========

I'm running DNX 1.0.0-beta6-11941. Here are the contents of my test project.json. When I don't include the TestHost and ApplicationHost dependencies, I can't connect to the TestHost:

{
    "version": "1.0.0-*",
    "dependencies": {
        "AdminTools": "1.0.0-*",
        "xunit": "2.1.0-*",
        "xunit.runner.dnx": "2.1.0-*",
        "Microsoft.Framework.TestHost": "1.0.0-*",
        "Microsoft.Framework.ApplicationHost": "1.0.0-*"
    },

    "frameworks": {
        "dnx451": { }
    },

    "commands": {
        "test": "xunit.runner.dnx"
    }
}

The contents of my test file:

    public class TestController
    {
        [Fact]
        public void PassingTest()
        {
            Assert.Equal(true, true);
        }
    }

Did you end up getting this resolved?

I ask because I was running into the same problem until I saw your post - which helped me fix my problem. I was missing Microsoft.Framework.ApplicationHost in the json. I think you might be missing xunit.runner.visualstudio.

Here is the dependencies section of project. json I used to get it working. First chunk might be superflous:

"dependencies": {
    "System.Collections": "4.0.10-beta-23019",
    "System.Linq": "4.0.0-beta-23019",
    "System.Threading": "4.0.10-beta-23019",
    "System.Runtime": "4.0.10-beta-23019",
    "Microsoft.CSharp": "4.0.0-beta-23019",

    "xunit": "2.1.0-*",
    "xunit.runner.dnx": "2.1.0-*",
    "xunit.runner.visualstudio": "2.0.1",
    "Microsoft.Framework.TestHost": "1.0.0-*",
    "Microsoft.Framework.ApplicationHost": "1.0.0-*"

I just ended up switching gears for a while. Now that .NET 5's rough schedule has been revealed, the company I'm working is going to go in a different direction with the application they are building.

Thanks for the feedback on this. If I end up coming back to this, I'll try this out.

Thanks @jderus worked for me.

This was most certainly a dependency issue. Updating to the latest builds should be all that's needed.