Check list with moq

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.

+4
source share
3 answers

Instead of using mock methods, you can use NUnit methods to make statements about the contents of a mock object.

After you have assigned a list to an object and confirmed that it has been installed, use assertions to check the specifics, such as counting the element, and that the first object matches what you expect from it.

 Assert.That(this._mockViewLoginPanel.Object.Staff.Length, Is.EqualTo(1)); Assert.That(this._mockViewLoginPanel.Object.Staff[0], Is.Not.Null); Assert.That(this._mockViewLoginPanel.Object.Staff[0], Is.EqualTo(loginStaff[0])); 

Edit

After further investigation, it comes down to Mock Behavior. The Staff and Person properties were not configured correctly.

Arrange them, change your layout creation to the following:

 var _mockViewLoginPanel = new Mock<IViewLoginPanel>(MockBehavior.Strict); _mockViewLoginPanel.SetupAllProperties(); 

Full list of demo codes:

 public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public interface IViewLoginPanel { IList<Person> Staff { get; set; } } public class PresenterLoginPanel { private IViewLoginPanel _iViewLoginPanel; public PresenterLoginPanel(IViewLoginPanel panel) { _iViewLoginPanel = panel; SomeFunction(); } public void SomeFunction() { List<Person> loginStaff = new List<Person>(); loginStaff.Add(new Person{FirstName = "John", LastName = "Doe"}); _iViewLoginPanel.Staff = loginStaff; } public IViewLoginPanel ViewLoginPanel { get { return _iViewLoginPanel; } } } [TestFixture] public class PresenterLoginPanelTests { [Test] public void When_The_Presenter_Is_Created_Then_All_CP_Staff_Is_Added_To_Dropdown() { var _mockViewLoginPanel = new Mock<IViewLoginPanel>(MockBehavior.Strict); _mockViewLoginPanel.SetupAllProperties(); PresenterLoginPanel target = new PresenterLoginPanel(_mockViewLoginPanel.Object); _mockViewLoginPanel.VerifySet(x => x.Staff = It.IsAny<List<Person>>()); Assert.AreEqual(5, _mockViewLoginPanel.Object.Staff.Count); } } 
+10
source

You can easily accomplish this using Moq itself (also when you don’t yet have a reference to the expected result object) - just use the It.Is(..) method:

 _mockViewLoginPanel.VerifySet(x => x.Staff = It.Is<List<Person>>(staff => staff.Count == 5)); _mockViewLoginPanel.VerifySet(x => x.Staff = It.Is<List<Person>>(staff => staff[0].FirstName == "John")); 
+6
source

This checks that the number of employees must be greater than 0, that there must be at least one element that is not zero, and there is at least one element that has a name equal to Joe. if you want to compare objects, you will need to add a comparator.

 Assert.AreNotEqual(this._mockViewLoginPanel.Object.Staff.Count, 0); Assert.AreNotEqual(this._mockViewLoginPanel.Object.Staff.All(x => x == null), true); Assert.AreEqual(this._mockViewLoginPanel.Object.Staff.Any(x => x.FirstName == "Joe"), true); 
+1
source

Source: https://habr.com/ru/post/1336071/


All Articles