c# - How to assert a default value of a property with FluentAssertions? -


i have class fields/properties etc., of various types:

public string somestringdata; public int? somenullableintegerdata; public somedataclass somespecificdata; public int someintegerdata; 

in part of code, somestringdata should filled, , want test explicitly that. use fluentassertions , have

actual.somestringdata.should().be("whatever"); 

but how test, none of other elements not touched , still have default value? could, of course:

actual.somenullableintegerdata.should().nothavevalue("must have no value."); actual.somedataclass.should().benull("must have no value."); actual.somenullableintegerdata.should().be(0, "must have no value."); 

but, how express more these items should have default value?

like example imaginary bedefault operator:

actual.somenullableintegerdata.should().bedefault("must have no value."); actual.somedataclass.should().bedefault("must have no value."); actual.somenullableintegerdata.should().bedefault("must have no value."); 

are there such options?

i create extension method

public static void shouldhavedefaultvalue<t>(this t value) {     if (!equalitycomparer<t>.default.equals(value, default(t)))         throw new assertionexception("must have default value."); } 

usage:

actual.somenullableintegerdata.shouldhavedefaultvalue(); actual.somedataclass.shouldhavedefaultvalue(); actual.someintegerdata.shouldhavedefaultvalue(); 

extending should() bedefault() difficult, because there different should() extensions return different assertions objects - booleanassertions, stringassertions, guidassertions etc. , assertions classes don't have common base class able extent.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -