Replies: 11 comments 1 reply
-
|
There are two overloads of public AndConstraint<TAssertions> Match(Expression<Func<TSubject, bool>> predicate, string because = "", params object[] becauseArgs);
public AndConstraint<TAssertions> Match<T>(Expression<Func<T, bool>> predicate, string because = "", params object[] becauseArgs) where T : TSubject;You probably just need to specify the type to hit the desired overload. new MyClass().Should().Match<MyClass>(e => e.MyProperty == 42); |
Beta Was this translation helpful? Give feedback.
-
|
@georgiosd Does this answer your question? |
Beta Was this translation helpful? Give feedback.
-
|
Hey, sorry that I didn't follow up. It does, but explicitly defining the type in Wondering how this hasn't come up before! Maybe I'm somehow an edge case :) |
Beta Was this translation helpful? Give feedback.
-
|
It boils down to the difficulty of designing a library based on extension methods. If we add |
Beta Was this translation helpful? Give feedback.
-
|
Ah, that makes sense. Because there's no constraint that could enforce non-IEnumerables. What if there was a method in |
Beta Was this translation helpful? Give feedback.
-
|
A specialization as in the existing If you mean something like o.Should().Specialize<Myclass>().Should().Match()then I prefer the current (and shorter) o.Should().Should().Match<MyClass>() |
Beta Was this translation helpful? Give feedback.
-
|
One that will trickle down the chain:
or even
|
Beta Was this translation helpful? Give feedback.
-
|
One could maybe do something like class GenericObjectAssertions<T> : ReferenceTypeAssertions<T, GenericObjectAssertions<T>>
{
public GenericObjectAssertions(object value)
{
if (value is null)
{
Subject = default(T);
}
else if (value is T castedValue)
{
Subject = castedValue;
}
else
{
throw new InvalidCastException();
}
}
}
GenericObjectAssertions<T> Should<T>(this object actualValue)
{
return new GenericObjectAssertions<T>(actualValue);
}That would be beneficial for ReferenceTypeAssertions.Match(Expression<Func<TSubject, bool>> predicate, ...)as the right but worse(?) for ReferenceTypeAssertions.BeSameAs(TSubject expected, ...)
ReferenceTypeAssertions.NotBeSameAs(TSubject unexpected, ...)as those are then "restricted" to |
Beta Was this translation helpful? Give feedback.
-
|
Yes, I see. But if it's a secondary method like |
Beta Was this translation helpful? Give feedback.
-
|
Another workaround is to define a your own assertion class that derives from private class MyClassAssertions : ReferenceTypeAssertions<MyClass, MyClassAssertions>
{
public MyClassAssertions(MyClass subject)
: base(subject)
{
}
protected override string Identifier => nameof(MyClass);
}
public static Extensions
{
public static MyClassAssertions Should(this MyClass myClass) =>
new MyClassAssertions(myClass);
} |
Beta Was this translation helpful? Give feedback.
-
|
This would also be beneficial for custom generic extensions like: Maybe something to consider for v7? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey guys,
I would swear this was always there but I guess I don't remember it correctly.
Right now we can do
o.Property.Should().Be(x)but noto.Should().Match(x => x.Property == x).And...I seem to remember there was a
ShouldHaveAPI that could do this?Thanks
Beta Was this translation helpful? Give feedback.
All reactions