Repository navigation
Closed
Description
An example
var donkey = new object();
var mule = new object();
Assert.That(new[] { donkey }, Has.Member(donkey).And.No.Member(mule));
Fails with this exception in 3.8.1
System.ArgumentException : The actual value must be an IEnumerable
Parameter name: actual
at NUnit.Framework.Constraints.SomeItemsConstraint.ApplyTo[TActual](TActual actual) in D:\git\nunit\src\NUnitFramework\framework\Constraints\SomeItemsConstraint.cs:line 64
at NUnit.Framework.Constraints.NotConstraint.ApplyTo[TActual](TActual actual) in D:\git\nunit\src\NUnitFramework\framework\Constraints\NotConstraint.cs:line 48
at NUnit.Framework.Constraints.AndConstraint.ApplyTo[TActual](TActual actual) in D:\git\nunit\src\NUnitFramework\framework\Constraints\AndConstraint.cs:line 64
at NUnit.Framework.Constraints.SomeItemsConstraint.ApplyTo[TActual](TActual actual) in D:\git\nunit\src\NUnitFramework\framework\Constraints\SomeItemsConstraint.cs:line 65
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args) in D:\git\nunit\src\NUnitFramework\framework\Assert.That.cs:line 245
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression) in D:\git\nunit\src\NUnitFramework\framework\Assert.That.cs:line 228
at Granta.Services.FunctionalData.Tests.ParameterValueExtraction.ParameterSingleValuesExtractorDatabaseTests.MethodUsingObjectsFailsInDifferentManner() in D:\git\servicelayer\FunctionalDataTests\ParameterValueExtraction\ParameterSingleValuesExtractorDatabaseTests.cs:line 59
This is a regression from 3.6 and I suspect was introduced by #2239.
The right hand constraint of the And is getting evaluated against each item of the array instead of against the enumerable. I have tried stepping through and figuring out where things go wrong but without success.
I presume I am using the API correctly - I expected the assertion to check that the array contained donkey and did not contain mule. If I am wrong, I would appreciate some guidance on how to chain these constraints using the fluent API - or should one write 2 separate assertions?
Activity
mikkelbu commentedon Sep 5, 2017
Before implementing a solution to this I think we should have tests that cover all the functionality of CollectionContainsConstraint - as it were in version 3.7 - and then use these tests to ensure that we have kept all the behaviour. I'll try to look into writing these tests tonight (if I have time). The tests should also cover #2412.
mikkelbu commentedon Sep 5, 2017
I've tried to go through all the changes done in #2239 and write tests for each change. All the tests pass in version 3.7.1, but in 3.8.1:
'SomeItemsConstraint' does not contain a definition for 'Using'), andSystem.ArgumentException : The actual value must be an IEnumerablehttps://gist.github.com/mikkelbu/370fe6a735c41906dca1fad28282f19e
Revert to original behaviour
mikkelbu commentedon Sep 19, 2017
@rprouse I've added this issue to the 3.8.2 milestone.
Snaaio commentedon Dec 4, 2017
I am currently using nunit.framework v3.9.0.0 and still have problems when using a Has.One.Member(...) constraint:
class TestElement { }Test fails with the following error:
Does this mean this issue is not completely fixed, is it a new issue or am I doing something wrong?
ChrisMaddock commentedon Dec 4, 2017
@Snaaio - would you file a new issue for this please? 🙂 I'm not entirely sure how the syntax should work here off the top of my head - is this a regression from a previous version of the framework?
CharliePoole commentedon Dec 4, 2017
I'm suspicious of the syntax
Has.One.Member, as opposed to simplyHas.Memberor evenHas.One.SameAs. It could be we are letting an unimplemented sequence through when it should not be allowed - or should be implemented if it is allowed.mikkelbu commentedon Dec 4, 2017
@Snaaio As far as I can tell from the PR you can write the assertion as
But note that this will also be satisfied by a collection with more than one element, as long as exactly one element is equal to
element. If you only want to match a collection with exactly one element that matches, then something likecan be used (note that I'm usually not using these long method sequences, so it can be that it can be written more succinct 😄 ).
@ChrisMaddock The
Has.Onesyntax is new in 3.8 and is a short hand forHas.Exactly(1).Items(see #2004).