A pleasant walk through computing

Comment for me? Send an email. I might even update the post!

Using FluentAssertions with xUnit Theory to Test for an Exception AND a Successful Return

I recently wanted to unit test a method that required significant setup, and where an invalid method argument would throw an exception while valid values returned easily testable results.

While I could have created a separate test, this really lent itself to using an xUnit Theory. The problem I faced was how to test for the exception but also test for a valid return. I didn't want to write duplicate code within the test itself, such as declaring the service twice.

Some research and experimentation led to the approach below. The trick is to declare a delegate function, then use FluentAssertions to either catch the invocation exception, or to invoke the function and return the value.

Here's a simple Class Library app to demonstrate the technique. I kind of love this because there's no wasted or duplicate code.

using System;
using FluentAssertions;
using Xunit;

namespace FluentAssertionsInvoking
{
    public class CustomerService_Should
    {
        [Theory]
        [InlineData(null)]
        [InlineData("Marcus Welby")]
        public void Return_a_customer_or_throw_an_exception(string name)
        {
            // arrange
            var expected = new Customer() { Name = name };
            var customerService = new CustomerService(expected);

            // act
            Func<CustomerService, Customer> action = service => service.GetCustomer();

            // assert
            // Exception condition
            if (name == null)
            {
                customerService.Invoking(action).Should().Throw<Exception>();
            }
            else
            // Valid condition
            {
                var result = action.Invoke(customerService);
                result.Should().BeEquivalentTo(expected);
            }
        }
    }

    public class Customer
    {
        public string Name { get; set; }
    }

    public class CustomerService
    {
        private Customer _customer;
        public CustomerService(Customer customer)
        {
            _customer = customer;
        }
        public Customer GetCustomer()
        {
            if (_customer.Name == null) { throw new Exception(); }
            return _customer;
        }
    }
}

References

Bonus

Here's the xunit.runner.json to show only method names in the Test Runner output. Remember to set the file to Copy to Output.

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "methodDisplay": "method"
}

Remote Work Tiny Tips - Last One: Overcoming Obstacles

It's relatively easy to get the technology running to work from home. The hard part is the doing. Don't beat yourself up! Try this tiny tip on...

Overcoming Obstacles

What if there were a simple technique, backed by science, that you could use in five minutes to help you improve all kinds of behaviors?

There is.

Mental Contrasting With Implementation Intentions - But You Can Call It WOOP

Mental Contrasting is a visualization technique involving first thinking of a positive future outcome followed by thinking of obstacles.

Implementation Intention is a self-regulatory strategy in the form of an "if-then plan" that can lead to better goal attainment. For example, "If I see the cookies, I'm going to grab the banana." Repeatedly imagining the plan improves the brain's automatic response.

Dr Gabriele Oettingen is premier among those who've researched MCII intensively, and she created a method for applying that research. It's called WOOP.

WOOP

Takes five minutes in a quiet (uninterrupted), calm environment, and intensely imagine the following.

  1. Your WISH (detailed goal with metrics)
  2. The best OUTCOME (all the positive stuff and feelings)
  3. Potential OBSTACLES (the negative stuff)
  4. Your If-Then PLAN

Do this every day, several times a day.

Important: Do not switch the "O"s. You must imagine the Outcome before the Obstacles. The research shows that switching them nullifies any benefit.

Here's Dr Oettingen describing the four steps.
The Four Steps of WOOP

And here she is taking people through the technique. I just love her.
Let’s WOOP

What's Going On? What About the Power of Postive Thinking?

Norman Vincent Peale got it (somewhat) wrong. It's not enough to think positively. In fact, trying to only think positively will hinder reaching your goals.

"Studies are showing that when people limit their thinking to imagining positive outcomes, they tend not to put forth the effort to make those outcomes come about. As [Dr Oettingen] put it:"

Positive thinking fools our minds into perceiving that we’ve already attained our goal, slackening our readiness to pursue it.

Understood this way, negative feedback is no longer a threat, it helps with how to fulfill the wish. It isn't personal. WOOP creates associative links in the brain, linking the outcome with the obstacle and the means to overcome the obstacle.

  • Mental contrasting has the better long-term benefit to achieving and maintaining goals
  • For easily attained goals, implementation intention doesn't matter much, but for difficult goals, It increases success threefold.
  • Practicing WOOP can double effectiveness on the goal ten weeks out.

WOOP isn't someone's opinion. It's not a gimmick. It's real science.

Finally

How much is five or ten minutes worth to you? Is it worth improving your life? Your kids' lives?

Go ahead. Find out.

References

Remote Work Tiny Tips - Staying Social

It's relatively easy to get the technology running to work from home. The hard part is the doing. Don't beat yourself up! Try this tiny tip on...

Staying Social

I heard a comment recently that went something like this (and which I used in a previous post):

Social distancing does not require social isolation

Today an acquaintance (thanks, Sean!) demonstrated a great method for staying social, especially if, like me, you're not inclined toward talking even when the government isn't mandating staying put. He interviewed me about music. We don't know each other well, but the interview format removes all kinds of social barriers. It was great. Plus, I love music.

The Simple (and Amazing!) Interview (That Doesn't Have to Be Long)

Remember: people love to talk about themselves

  1. Write down a subject you're curious about that you think other people will know about
  2. Write down three questions about that subject
  3. Write down three people you could call
  4. Email or text one of them to set up a formal date/time for the interview. Don't be late!
  5. Video chat or call. Do not email, do not text. You must hear their voice, and ideally see their face.
  6. Ask your questions. Record if you want to, but be sure to ask if that's OK.
  7. Listen. Really listen. This person is giving you a great gift.
  8. Afterward, immediately email and thank them for their time.
  9. Then...and this is the goose-pimply, sipping-bourbon-in-winter part--savor what you just did. Write down how you felt, what your favorite answers were, what you learned.

Finally

Humans need connection. Positive social connections are a strong predictor of length and quality of life.

Go ahead. Book that interview.