Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to generate chained assertions #38

Open
MichaelBitard opened this issue Mar 24, 2017 · 3 comments
Open

Add option to generate chained assertions #38

MichaelBitard opened this issue Mar 24, 2017 · 3 comments

Comments

@MichaelBitard
Copy link

Enhancement

Use case:

  • You have an object "objectA" of type ObjectA which contains (among other fields) a field objectB of type ObjectB
  • To assert it, I do
assertThat(objectA).hasFieldOfObjectA("field");
assertThat(objectA.objectB).hasFieldOfObjectB("fieldOfObjectB");
  • I'd like to be able to assert like this:
assertThat(objectA)
  .hasFieldOfObjectA("field")
  .withObjectB()
    .hasFieldOfObjectB("fieldOfObjectB");

I already do that manually, but I think it could be useful to be able to add this behavior automatically.

What do you think?

@joel-costigliola
Copy link
Member

I agree on the idea, I would just use getB() to navigate to the B's assertions.

@yoorick
Copy link

yoorick commented Oct 9, 2018

I would rather prefer to see it as has<FieldName>Satisfying(Consumer/Condition) pair of methods. Just like hasValueSatisfying for Optional or satisfies for list elements or other similar asserts. This way you can continue to fluently verify other complex fields of objectA. With getB()/withObjectB() approach you have no way to fluently return back to objectA assertions.

@yoorick
Copy link

yoorick commented Oct 9, 2018

Something like this:

assertThat(objectA)
    .hasFieldOfObjectA("field")
    .hasObjectBSatisfying(
        objectB -> assertThat(objectB)
            .hasFieldB1("value1")
            .hasFieldB2("value2")
    )
    .hasObjectCSatisfying(
        objectC -> assertThat(objectC)
            .hasFieldC1("otherValue1")
            .hasFieldC2(1234)
            .hasFieldC3("otherValue3")
    )
    // ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants