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.

Add new namespace assertions to TypeSelectorAssertions

See original GitHub issue

Description

Currently, TypeSelectorAssertions only contains methods for asserting that types are (not) decorated with specific attributes, e.g., BeDecoratedWith<>. However, TypeSelector contains more selectors that would also be useful as assertions.

One such group of selectors filter on the namespace of types and contain the methods ThatAreInNamespace, ThatAreNotInNamespace, ThatAreUnderNamespace, and ThatAreNotUnderNamespace. Corresponding assertions could be named BeInNamespace, NotBeInNamespace, BeUnderNamespace, and NotBeUnderNamespace.

Use case

A concrete use case I have that prompted this suggestion is that the JsonSubTypes library, which is used to deserialize JSON into one of a number of subclasses based on a discriminator value, only supports deserializing into subclasses in the same namespace as the base class. Therefore, I would like to be able to write a test that asserts that all subclasses of a specific class are in the same namespace as it, à la:

AllTypes.From(baseClassAssembly).ThatDeriveFrom<BaseClass>().Should().BeInNamespace(baseClassNamespace);

Draft implementation as custom extension

To support the use case above, I have implemented the BeInNamespace assertion in our project as the following custom extension:

public static class TypeSelectorAssertionsExtensions
{
    public static AndConstraint<TypeSelectorAssertions> BeInNamespace(this TypeSelectorAssertions typeSelectorAssertions, string @namespace, string because = "", params object[] becauseArgs)
    {
        Execute.Assertion
               .BecauseOf(because, becauseArgs)
               .ForCondition(@namespace != null)
               .FailWith("Types cannot have a null namespace")
               .Then
               .Given(() => typeSelectorAssertions.Subject)
               .ForCondition(types => types.All(type => type.Namespace == @namespace))
               .FailWith("Expected types to be in namespace {0}{reason}, but found {1}.",
                   _ => @namespace, types => types.Where(type => type.Namespace != @namespace));

        return new AndConstraint<TypeSelectorAssertions>(typeSelectorAssertions);
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Nov 30, 2019

I’ve opened #1196 to track issue with ThatAreUnderNamespace matching partial namespaces.

0reactions
kimsey0commented, Dec 6, 2019

Thanks for fixing the partial and null namespace issues in #1197. (I never got around to creating the issue for it. Sorry.)

Now that it’s merged, I’ll get #1180 up to date and ready to merge.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Releases - Fluent Assertions
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 >
Configure Namespaces - TechDocs - Broadcom Inc.
Using the custom mapping feature, a namespace map can be defined and saved in the Evaluate Request XPath assertion.
Read more >
Fluent Assertion namespace not recognised
Net version 4.6.1 and have installed fluent assertion 3.3.0 on an MVC4 project, but the namespace is not being recognised in the Unit...
Read more >
XML Namespace Document for Test Assertions Part 2
This document describes the XML namespace name(s) declared in the specification Test Assertions Part 2 - Test Assertion Markup Language Version 1.0 ...
Read more >
Chocolatey Software | crane 0.2.0.15
To install crane, run the following command from the command line or from PowerShell: >. To upgrade crane, run the following command from...
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