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

DLR/dynamic support #234

Closed
artkpv opened this issue Feb 18, 2015 · 11 comments
Closed

DLR/dynamic support #234

artkpv opened this issue Feb 18, 2015 · 11 comments

Comments

@artkpv
Copy link

artkpv commented Feb 18, 2015

Hi,
Is there a way to make FluentAssertions work with dynamic/DLR? Consider the following code:

dynamic expando = new ExpandoObject();
expando.someVar = "string";
expando.someVar.Should().Be("string");

which throws Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: "'string' does not contain a definition for 'Should'"

Thanks!

@dennisdoomen
Copy link
Member

Not until C# supports calling extension methods from dynamic objects...

@adamvoss
Copy link
Contributor

Usually dynamic users can still utilize Extension Methods by calling them as static methods. Can anything reasonably be changed internally in FA to support this?

As is a StackOverflowException is thrown by the following example. I only assume that it managed to get caught in some sort of infinite recursion between overloaded methods.

string d = "hi";
AssertionExtensions.Should(d).Be("yo");

Given a number of the recent .NET targets do not support dynamic I am not sure it is oft used enough that FA needs cater to it. However, I believe it would at least be possible to at least prevent stack overflow by adding a Should(dynamic) method on platforms that support it.

Since NUnit's Assert.That constraint-based assertions do depend on the objects' types you might find those more suitable. Alternatively, you could of course cast your dynamic object to be able to us FA's assertions.

@dennisdoomen
Copy link
Member

@vossad01 But what should Should(dynamic) do?

@JowenMei
Copy link

Can't you use the equality operator, like in Assert.AreEqual()?
I'm also looking for a fluent alternative for:
Assert.AreEqual(controller.ViewBag.HelpUrl, "helpUrl value from settings");

@mpodonyi
Copy link

Is it not possible to use .As<T>() or .BeOfType<>() method to cast the dynamic value to a concrete one.
e.g:

dynamic set = GetDynamic();
set.As<int>().Should().Be(6);

Looks better than:

dynamic set = GetDynamic();
((int)set).Should().Be(6);

@artkpv
Copy link
Author

artkpv commented Dec 16, 2015

@mpodonyi That .As<T>() does not work. Throws Microsoft.CSharp.RuntimeBinder.RuntimeBinderException. As @dennisdoomen wrote DLR does not support extension methods so far. Here is interesting discussion. In short it can't search the method at runtime to match the object.

@TroySijuwade
Copy link

@artkpv , @JowenMei I note this is over 2 years late ;) ... and for the benefit of those still trying to figure out how to do this, I was able to do this using FluentAssertions:

((string) result.ViewBag.HelpUrl).ShouldBeEquivalentTo("helpUrl value from settings");

@dennisdoomen
Copy link
Member

The situation will be a bit better with 5.0 because the object being compared with will be what is used to traverse the object graph instead of the SUT.

@WhatFreshHellIsThis
Copy link

WhatFreshHellIsThis commented Feb 9, 2018

Has anything now changed with 5.0? (I need to test dynamic objects with a REST api)
I want to check if a dynamic object has a property or not for starters.

@dennisdoomen
Copy link
Member

Unless there's a way to define a Should() extension method that acts on dynamic, no

@lycandroid
Copy link

I am using cast to object as a workaround, but where I am specifically wanting to test the type of a dynamic e.g.

// field.value is dynamic
field.value = "xyz";
((object)field.value).Should().BeOfType(typeof(string));

FWIW the same cast would work for the OP

 ((object)expando.someVar).Should().Be("string");

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

No branches or pull requests

8 participants