question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ContainItemsAssignableTo vs AllBeAssignableTo

See original GitHub issue

Description

Is there any difference between ContainItemsAssignableTo and AllBeAssignableTo? Spending five minutes looking at them, they seem to test the same scenario. (With the exception, that AllBeAssignableTo has special handling for IEnumerable<Type>)

In contrast to other Contain methods, ContainItemsAssignableTo expects all elements to satisfy and not just a subset.

Complete minimal example reproducing the issue

class Base { }
class Derived : Base { }

public void MyTestMethod()
{
    new[] { new Derived(), new Base() }.Should().AllBeAssignableTo<Derived>();  // fails as expected
    
    new[] { new Derived(), new Base() }.Should().ContainItemsAssignableTo<Derived>(); // <-- I would have expected this to pass
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
eNeRGy164commented, Jun 29, 2021

These are different assertions.

subject.Should().ContainItemsAssignableTo<T>()
    .Which.Should().ContainSingle();

Collection can contain multiple types, but exactly 1 is matching the specified type

collection.Should().ContainSingle()
    .Which.Should().BeOfType<T>();

Collection can only contain a single item, and it should match the specified type

0reactions
siewerscommented, Jun 29, 2021

@eNeRGy164 Yes, you are right of course 😃 I still think that scenario reads a bit messy. I would never talk like that, and reading it as

subject should contain items assignable to type T, of which there should be only one

versus

subject should contain only one item of type T

, I know which one I would prefer.

However, I am not sure how to express this fluently, without breaking existing convention.

subject.Should().Contain().AtMost(1).OfType<T>();
subject.Should().Contain().AtLeast(1).OfType<T>();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Collections
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit...
Read more >
Releases - Fluent Assertions
Fixed hanging of CompleteWithinAsync when used with WithResult and ... ContainItemsAssignableTo now expects at least one item assignable to T - #1765 ...
Read more >
(T81-17)用FluentAssertions的ContainItemsAssignableTo
... 的 ContainItemsAssignableTo 、 AllBeAssignableTo 、AllBeOfType測試 ... LeetCode 34 Find First and Last Position of Element in Sorted Array.
Read more >
FluentAssertions Collection集合类型断言
And.ContainItemsAssignableTo<int>(); // 元素可以被赋值成int类型 collection. ... AllBeAssignableTo<Student>(); // 是不是子元素都是Student类型 students1.
Read more >
fluentassertions中文文档_weixin_30451709的博客
And.ContainItemsAssignableTo<int>();. collection. ... ContainItemsAssignableTo<int>();. collection. ... AllBeAssignableTo<DerivedType>();.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found