pose: NullReferenceException in Pose.Helpers.StubHelper.GetMatchingShimIndex
Using Pose in a fairly complex test I’ve been getting this exception. It seems to be trying to find a shim for method(s) that haven’t been shimmed. Unfortunately I’ve not been able to reproduce this in a simple example and can’t post the full code from my product.
The test project is using an in-memory ASP.NET Core server (https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing) and the method I’m shimming is called from the Controller, not directly from the unit test itself. I expect this is the underlying problem as I’m calling PoseContext.Isolate from the “client” and expecting it to maintain that isolation on the “server” side.
Anyway, I’m posting this anyway in case you have any suggestions.
Unit test:
#if !NET462
[Fact]
public void When_person_exists_in_db()
{
var sqlMapperShim = Shim.Replace(() => SqlMapper.QueryAsync<Applicant>(Is.A<IDbConnection>(), Is.A<CommandDefinition>()))
.With(
delegate (IDbConnection dbConnection, CommandDefinition commandDefinition)
{
return Task.FromResult(SqlMapper.Query<Applicant>(dbConnection, commandDefinition));
});
Default<ApplicantListViewModel> response = null;
PoseContext.Isolate(() =>
{
var httpResponse = this.Read($"organisations/{TestConstants.OrganisationIdentifier}/applicants");
response = httpResponse.Content.ReadAsAsync<Default<ApplicantListViewModel>>().Result;
},
sqlMapperShim);
// Assert
response.ShouldNotBe(null);
}
#endif
Test results:
Test run for C:\Code\MyCompany\MyProduct\test\MyCompany.MyProduct.Specs\bin\Debug\netcoreapp2.0\MyCompany.MyProduct.Specs.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.3.0-preview-20170628-02
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
[xUnit.net 00:00:01.4649449] Discovering: MyCompany.MyProduct.Specs
[xUnit.net 00:00:01.5546231] Discovered: MyCompany.MyProduct.Specs
[xUnit.net 00:00:01.5617069] Starting: MyCompany.MyProduct.Specs
[xUnit.net 00:00:53.7768297] MyCompany.MyProduct.Specs.ApplicantsSpecs.ApplicantListSpecs.When_person_exists_in_db2 [FAIL]
[xUnit.net 00:00:53.7786980] System.NullReferenceException : Object reference not set to an instance of an object.
[xUnit.net 00:00:53.7800539] Stack Trace:
[xUnit.net 00:00:53.7811621] C:\Code\MyCompany\MyProduct\test\MyCompany.MyProduct.Specs\Pose\IL\MethodRewriter.cs(27,0): at Pose.IL.MethodRewriter.Rewrite()
[xUnit.net 00:00:53.7813452] at stub_System.Net.Http.Headers.HttpHeaders_ParseRawHeaderValues(HttpHeaders , String , HeaderStoreItemInfo , Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7814211] at dynamic_System.Net.Http.Headers.HttpHeaders_AddHeaders(HttpHeaders , HttpHeaders )
[xUnit.net 00:00:53.7814574] at stub_System.Net.Http.Headers.HttpHeaders_AddHeaders(HttpHeaders , HttpHeaders , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7814902] at dynamic_System.Net.Http.Headers.HttpRequestHeaders_AddHeaders(HttpRequestHeaders , HttpHeaders )
[xUnit.net 00:00:53.7815276] at stub_System.Net.Http.Headers.HttpHeaders_AddHeaders(HttpHeaders , HttpHeaders , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7815669] at stub_System.Net.Http.HttpClient_PrepareRequestMessage(HttpClient , HttpRequestMessage , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7816352] at dynamic_System.Net.Http.HttpClient_SendAsync(HttpClient , HttpRequestMessage , HttpCompletionOption , CancellationToken )
[xUnit.net 00:00:53.7816805] at stub_System.Net.Http.HttpClient_SendAsync(HttpClient , HttpRequestMessage , HttpCompletionOption , CancellationToken , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7817409] at dynamic_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption , CancellationToken )
[xUnit.net 00:00:53.7817783] at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption , CancellationToken , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7818927] at dynamic_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption )
[xUnit.net 00:00:53.7819275] at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7820015] at dynamic_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri )
[xUnit.net 00:00:53.7820570] at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7821121] at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , String , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7821604] at dynamic_MyCompany.MyProduct.Specs.BehavesLikeApi_Read(BehavesLikeApi , String , Dictionary`2 , Dictionary`2 )
[xUnit.net 00:00:53.7822409] at stub_MyCompany.MyProduct.Specs.BehavesLikeApi_Read(BehavesLikeApi , String , Dictionary`2 , Dictionary`2 , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:53.7823764] at dynamic_MyCompany.MyProduct.Specs.ApplicantsSpecs.ApplicantListSpecs+<>c__DisplayClass3_0_<When_person_exists_in_db2>b__1(<>c__DisplayClass3_0 )
Failed MyCompany.MyProduct.Specs.ApplicantsSpecs.ApplicantListSpecs.When_person_exists_in_db2
Error Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
at Pose.IL.MethodRewriter.Rewrite() in C:\Code\MyCompany\MyProduct\test\MyCompany.MyProduct.Specs\Pose\IL\MethodRewriter.cs:line 27
at stub_System.Net.Http.Headers.HttpHeaders_ParseRawHeaderValues(HttpHeaders , String , HeaderStoreItemInfo , Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Net.Http.Headers.HttpHeaders_AddHeaders(HttpHeaders , HttpHeaders )
at stub_System.Net.Http.Headers.HttpHeaders_AddHeaders(HttpHeaders , HttpHeaders , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Net.Http.Headers.HttpRequestHeaders_AddHeaders(HttpRequestHeaders , HttpHeaders )
at stub_System.Net.Http.Headers.HttpHeaders_AddHeaders(HttpHeaders , HttpHeaders , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.Net.Http.HttpClient_PrepareRequestMessage(HttpClient , HttpRequestMessage , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Net.Http.HttpClient_SendAsync(HttpClient , HttpRequestMessage , HttpCompletionOption , CancellationToken )
at stub_System.Net.Http.HttpClient_SendAsync(HttpClient , HttpRequestMessage , HttpCompletionOption , CancellationToken , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption , CancellationToken )
at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption , CancellationToken , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption )
at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , HttpCompletionOption , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri )
at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , Uri , RuntimeMethodHandle , RuntimeTypeHandle )
at stub_System.Net.Http.HttpClient_GetAsync(HttpClient , String , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_MyCompany.MyProduct.Specs.BehavesLikeApi_Read(BehavesLikeApi , String , Dictionary`2 , Dictionary`2 )
at stub_MyCompany.MyProduct.Specs.BehavesLikeApi_Read(BehavesLikeApi , String , Dictionary`2 , Dictionary`2 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_MyCompany.MyProduct.Specs.ApplicantsSpecs.ApplicantListSpecs+<>c__DisplayClass3_0_<When_person_exists_in_db2>b__1(<>c__DisplayClass3_0 )
[xUnit.net 00:01:10.9337665] Finished: MyCompany.MyProduct.Specs
Total tests: 47. Passed: 46. Failed: 1. Skipped: 0.
Test Run Failed.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 15 (6 by maintainers)
I’ve noticed that
Assertstatements tend to break Pose. I’m currently looking into it, but in the meantime you can do this instead: