Skip to content
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

Should().Be() Throws exception using dynamic types #1493

Closed
ghost opened this issue Feb 10, 2021 · 4 comments
Closed

Should().Be() Throws exception using dynamic types #1493

ghost opened this issue Feb 10, 2021 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 10, 2021

Description

Should().Be() throws an exception when the types being tested are dynamic.

[TestMethod]
public void ExceptionWithDynamic()
{
    dynamic myDynamic1 = 1;
    dynamic myDynamic2 = 1;
    myDynamic1.Should().Be(myDynamic2);
}

Gives the exception

ExceptionWithDynamic
   Source: UnitTest1.cs line 2206
   Duration: 329 ms

  Message: 
    Test method VBALibrary.ExceptionWithDynamicDemo.ExceptionWithDynamic threw exception: 
    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'int' does not contain a definition for 'Should'
  Stack Trace: 
    CallSite.Target(Closure , CallSite , Object )
    UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
    ExceptionWithDynamicDemo.ExceptionWithDynamic() line 2210

If I replace the dynamic with int all is fine.

public class ExceptionWithDynamicDemo
{
    [TestMethod]
    public void ExceptionWithDynamic()
    {
        int myDynamic1 = 1;
        int myDynamic2 = 1;
        myDynamic1.Should().Be(myDynamic2);
    }
}
@jnyrup
Copy link
Member

jnyrup commented Feb 10, 2021

Have you had a look at #234 and #473?

@ghost
Copy link
Author

ghost commented Feb 10, 2021

I guess I've been spoiled by the Rubberduck repository which pops up related posts when you enter the title for an issue.

My other thoughts would be that as dynamic appears to be problematical there should be some kind of discussion of the issues on the help pages.

If I were authoring the code, I'd be trying to forwarn people of the issues with a specific message rather than relying on an exception message.

@jnyrup
Copy link
Member

jnyrup commented Feb 10, 2021

I agree that a note in the documentation would be nice, we're open for PR's ;)

We're not relying on any exceptions (as I see it), it's how C# handles extension methods on dynamic types.

See this minimal example that not use Fluent Assertions, but behaves similar to your example.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        dynamic obj = 1;
        obj.Should();
    }
}

public static class Extensions
{
    public static object Should(this object o)
    {
        throw new System.Exception("this is never hit");
    }
}

image

@jnyrup
Copy link
Member

jnyrup commented Feb 11, 2021

The best you can probably do is to cast your dynamic object to object before calling Should().

dynamic obj = 1;
((int)obj).Should();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant