Quantcast
Channel: Fluent Assertions
Viewing all articles
Browse latest Browse all 1402

New Post: Error when validating a list

$
0
0

I am trying to validate that 1 collection is equivalent to another collection.  The validation is failing with the following error...

Expected collection {

MyCoolApp.Business.Models.StateAbbreviation
{
   StateAbbr = "S1"
   StateName = "State 1"
}, 

MyCoolApp.Business.Models.StateAbbreviation
{
   StateAbbr = "S2"
   StateName = "State 2"
}, 

MyCoolApp.Business.Models.StateAbbreviation
{
   StateAbbr = "S3"
   StateName = "State 3"
}} to be equivalent to {

MyCoolApp.Business.Models.StateAbbreviation
{
   StateAbbr = "S1"
   StateName = "State 1"
}, 

MyCoolApp.Business.Models.StateAbbreviation
{
   StateAbbr = "S2"
   StateName = "State 2"
}, 

MyCoolApp.Business.Models.StateAbbreviation
{
   StateAbbr = "S3"
   StateName = "State 3"
}}.

at FluentAssertions.Frameworks.LateBoundTestFramework.Throw(String message) in c:\Workspaces\FluentAssertions\Releases\1.7.0\FluentAssertions.Net35\Frameworks\LateBoundTestFramework.cs: line 16
at FluentAssertions.Verification.FailWith(String failureMessage, Object[] failureArgs) in c:\Workspaces\FluentAssertions\Releases\1.7.0\FluentAssertions.Net35\Verification.cs: line 158
at FluentAssertions.Assertions.CollectionAssertions`2.BeEquivalentTo(IEnumerable expected, String reason, Object[] reasonArgs) in c:\Workspaces\FluentAssertions\Releases\1.7.0\FluentAssertions.Net35\Assertions\CollectionAssertions.cs: line 453
at MyCoolApp.UnitTests.ControllerTests.HomeControllerTests.When_accessing_the_logon_page.It_should_return_the_logon_page_on_a_new_request() in When_accessing_the_logon_page.cs: line 60
My test method looks like this...
		[Test]
		public void It_should_return_the_logon_page_on_a_new_request()
		{
			//Arrange
			var stateAbbreviations = new List<StateAbbreviation>
			                         	{
			                         		new StateAbbreviation { StateName = "State 1", StateAbbr = "S1" },
			                         		new StateAbbreviation { StateName = "State 2", StateAbbr = "S2" },
			                         		new StateAbbreviation { StateName = "State 3", StateAbbr = "S3" }
			                         	};

			Mock.Arrange(() => _DataContext.StateAbbreviations).Returns(stateAbbreviations.AsQueryable());

			//Act
			var result = (ViewResult) _HomeController.LogOn();
			var model = result.Model as LogonViewModel;

			//Assert
			Mock.AssertAll(_DataContext);

			result.ViewName.Should().BeBlank();
			model.Should().NotBeNull();
			model.StateAbbreviationDropDownList.Select(x => new StateAbbreviation { StateAbbr = x.Value, StateName = x.Text}).Should().BeEquivalentTo(stateAbbreviations);
		}
And the Controller action looks like this...
		public ActionResult LogOn()
		{
			var viewModel = new LogonViewModel();

			var stateAbbreviations = _DataContext.StateAbbreviations.ToList();

			stateAbbreviations.ForEach(x =>
				{
					var dropDownItem = new DropDownItem();
					dropDownItem.Value = x.StateAbbr;
					dropDownItem.Text = x.StateName;
					viewModel.StateAbbreviationDropDownList.Add(dropDownItem);
				});

			return View(viewModel);
		}

Viewing all articles
Browse latest Browse all 1402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>