|
|
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);
}
|
|
Coordinator
May 15, 2012 at 6:58 AM
|
That's because collection.Should().BeEquivalentTo() relies on the Equals() implementation of those items. And if you don't explicitly implement Equals(), it will rely on the default .NET Framework provided implementation that uses a reference comparision.
So your observed behavior is perfectly ok.
So either implement Equals() and GetHashCode(), or use the object.ShouldHave().AllProperties.EqualTo(). You do need 1.7.1 though, because that one introduces support for comparing collections of objects based on their proeprties.
|
|
|
|
I knew it was something simple. Thanks!!
Sent from my iPhone
From: dennisdoomen
That's because collection.Should().BeEquivalentTo() relies on the Equals() implementation of those items. And if you don't explicitly implement Equals(), it will rely on the default .NET Framework provided implementation that uses a reference comparision.
So your observed behavior is perfectly ok.
So either implement Equals() and GetHashCode(), or use the object.ShouldHave().AllProperties.EqualTo(). You do need 1.7.1 though, because that one introduces support for comparing collections of objects based on their proeprties.
|
|