c# - How to check if Dictionary of type class can be verified using ContainValue in Fluent Assertion -
i trying check dictionary of type custom class value.
i know if approach of searching value within dictionary right using containvalue in fluent assertion.
here code
public static void verifydictionary() { seek { dictionary<int, employee> empdict = new dictionary<int, employee> { { 1, new employee() {employeeid = 100,name="karthik", employeeemail="karthik@executeautomation.com", employeeaddress="chennai,india"}}, {2, new employee() { employeeid = 101,name="jack",employeeemail="jack@jill.com",employeeaddress="ca"} }, {3, new employee() { employeeid=102,name="sam",employeeemail="sam@sung.com",employeeaddress="sfo"}}, {4, new employee() { employeeid=103,name="max",employeeemail="micro@max.com",employeeaddress="india" }} }; empdict.should().containvalue(new employee() { employeeid = 10, employeeemail = "micro@max.com", employeeaddress = "india", name = "max" }, "because thats info need"); } grab (exception e) { console.writeline(e.message); } } employee class
public class employee { public int employeeid { get; set; } public string name { get; set; } public string employeeemail { get; set; } public string employeeaddress { get; set; } } while execute code, getting exception shown
**
expected dictionary {[1, bdddemo.employee], [2, bdddemo.employee], [3, bdddemo.employee], [4, bdddemo.employee]} contain value bdddemo.employee { employeeaddress = "india" employeeemail = "micro@max.com" employeeid = 10 name = "max" } because thats info need. **
please help me understand whats wrong doing here.
thanks,
containvalue's check whether value in dictionary or not based on equals method of values in dictionary.
in case employee not override equals implementation inherited object used; reference equality used.
as example, next code pass:
class="lang-cs prettyprint-override">var expected = new employee { employeeid = 10 }; var dictionary = new dictionary<int, employee> { {1, expected} }; dictionary.should().containvalue( expected ); this, however, fail because 2 different instances of employee though members same:
var expected = new employee { employeeid = 10 }; var dictionary = new dictionary<int, employee> { {1, expected} }; dictionary.should().containvalue( new employee { employeeid = 10 } ); to create later case pass override equals. however, entire class mutable cannot recommend unless create whichever member(s) using determine equality immutable.
without modifying class, best bet utilize different assertion, along these lines:
class="lang-cs prettyprint-override">dictionary.values.should().contain(employee => /* logic how want determine match */); c# assertions fluent-assertions
No comments:
Post a Comment