Given the call code
List<Person> loginStaff = new List<Person>(); loginStaff.add(new Person{FirstName = "John", LastName = "Doe"}); this._iViewLoginPanel.Staff = loginStaff;
What is the syntax for checking that there is a state named john doe and that there is at least one state that is installed? Currently, all the examples I saw are pretty simple, using only It.IsAny or Staff = some basic type, but no one actually checks the data in complex types such as lists.
My statements look like
this._mockViewLoginPanel.VerifySet(x=> x.Staff = It.IsAny<List<Person>>());
which only checks the type specified by the installer, but not the size or elements in the list itself. I tried to do something like this:
this._mockViewLoginPanel.VerifySet( x => { List<string> expectedStaffs = new List<string>{"John Doe", "Joe Blow", "AA", "Blah"}; foreach (Person staff in x.Staff) { if (!expectedStaffs.Contains(staff.FirstName + " " + staff.LastName)) return false; } return true; });
But this tells me that the body of the lambda operator cannot be converted to an expression tree. Then I had an idea to put the body of an expression in a function and run it, but at runtime I get:
System.ArgumentException: expression is not a setterter call.
Update: In light of the first two responses to using assert, I tried this method, but found that even after setting Staff to a non-null list, it still appears in debug as null. So this is what the full test looks like.
[TestMethod] public void When_The_Presenter_Is_Created_Then_All_CP_Staff_Is_Added_To_Dropdown() { this._mockViewLoginPanel = new Mock<IViewLoginPanel>(); PresenterLoginPanel target = new PresenterLoginPanel(this._mockViewLoginPanel.Object); this._mockViewLoginPanel .VerifySet(x => x.Staff = It.IsAny<List<Person>>()); Assert.AreEqual(5, this._mockViewLoginPanel.Object.Staff.Count); }
And somewhere inside the PresenterLoginPanel constructor
public PresenterLoginPanel { private IViewLoginPanel _iViewLoginPanel; public PresenterLoginPanel(IViewLoginPanel panel) { this._iViewLoginPanel = panel; SomeFunction(); } SomeFunction() { List<Person> loginStaff = new List<Person>(); loginStaff.add(new Person{FirstName = "John", LastName = "Doe"}); this._iViewLoginPanel.Staff = loginStaff; } }
When I debug the next line, this._iViewLoginPanel.Staff is null, which causes a null exception in the statement.