-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NullReferenceException in Pose.Helpers.StubHelper.GetMatchingShimIndex #3
Comments
@tjrobinson if the client test actually calls the code in the controller instead of making an actual http request you should be fine. I'm looking into this as well as the other issues you raised. Have you been able to get some other tests to work with Pose? |
Also what's happening here is that a stub is passing a null method to be rewritten. |
Yep, I think it would be. Unfortunately these are deliberate integration tests to check the API layer. I have other tests for the MediatR commands/queries that the controllers use. I haven't got any other tests working with Pose yet as I've not tried yet. |
Alrighty. Do try if/when you can and let me know if you run into anymore problems. Thanks |
@tjrobinson I'm curious, what made you think the error was from |
@tonerdo I can't try the |
@tonerdo I have the same Null Reference Exception popping up. And tracked it down to The following code throws the null reference exception.
It was trying to find: After that I was getting confused. Hope this helps. |
I've noticed that using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Pose.Tests.Fails
{
[TestClass]
public class ShimFails
{
public class MyClass
{
public int MyProperty { get; set; }
}
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void ShimConstructor()
{
Shim ctorShim = Shim.Replace(() => new MyClass())
.With(() => new MyClass { MyProperty = 10 });
int prop = 0;
PoseContext.Isolate(() =>
{
prop = new MyClass().MyProperty;
}, ctorShim);
// this line breaks the Shim
Assert.AreEqual(10, prop);
}
}
} |
I tried looking into the issue. Unfortunately I haven't found a solution yet. I tried the following:
The name of the method it find is: "System.Reflection.IReflectableType.GetTypeInfo" (this is not the But running this results in crashing the debugger.
|
@jonkeda Problem with Assert.AreEqual caused by explicit implementation of IReflectableType.GetTypeInfo() in class System.Reflection.IntrospectionExtensions
{
public static TypeInfo GetTypeInfo(this Type type)
{
if (type == (Type) null)
throw new ArgumentNullException(nameof (type));
return ((IReflectableType) type).GetTypeInfo();
}
} this is the reason why there is a case to reproduce this issue interface IMyTest
{
int ExplicitMethod();
}
class MyTest : IMyTest
{
int IMyTest.ExplicitMethod() => 1;
}
[TestMethod]
public void TestExplicit()
{
Shim shim = Shim.Replace(() => Is.A<IMyTest>().ExplicitMethod()).With((IMyTest t) => 42);
var r = new MyTest();
PoseContext.Isolate(() => { var foo = ((IMyTest)r).ExplicitMethod(); }, shim);
} I haven't solve this problem yet but if your know an elegant and easy way to get explicitly implemented members, i would like to hear about it |
@Daxaker It isn't anywere near elegant, but I do have a working 'solution'.
This code works for your testcase. But isn't very elegant or fail safe. |
@Daxaker many thanks for this repro. Will take a look and work on a fix |
@tonerdo Glad to be helpful |
My issue indeed had to do with calling xUnit Assert() method within the Isolate() function. Moving my assert methods out fixed the error for me. |
Has this problem fixed in a new distribution? |
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:
Test results:
The text was updated successfully, but these errors were encountered: