Restrict custom assertion to a specific (reference) type while keeping existing object assertions #1999
-
Hi there, I've trying to write a custom assertion for a class. I've tried following this discussion about extending existing assertions but I didn't get the results that I've hoped for. Consider the following code. public class MyClass { }
public static class MyClassExtensions
{
public static AndConstraint<ObjectAssertions> BeTheBestClassEver(this ObjectAssertions parent,
string because = "", params object[] becauseArgs)
{
// ...
}
} This kind of works. I don't lose existing object assertions on my class (such as new MyClass().should().BeTheBestClassEver(); The problem is that I can also do this: new { }.should().BeTheBestClassEver(); How can I restrict this custom assertion to my specific type, while keeping existing assertion? I've looked into using the generic version What's the correct way to implement a custom assertion that can be applied on a specific type? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This might have been the wrong approach here. I only "explored" alternative solutions because following the official documentation on creating custom assertions did not yield the expected result. What I had written initially was: public class MyClassAssertions : ReferenceTypeAssertions<MyClass, MyClassAssertions>
{
public MyClassAssertions(MyClass instance) : base(instance) { }
public AndConstraint<MyClassAssertions> BeTheBestClassEver(string because = "", params object[] becauseArgs)
{
// ...
}
} This meant that all assertions defined in public class MyClassAssertions : ObjectAssertions<MyClass, MyClassAssertions>
{
} My conclusion is that extending |
Beta Was this translation helpful? Give feedback.
This might have been the wrong approach here. I only "explored" alternative solutions because following the official documentation on creating custom assertions did not yield the expected result. What I had written initially was:
This meant that all assertions defined in
ObjectAssertions
were basically removed from my class. To get keep existing assertions and get my new, custom assertion for my…