Can this test be refactored to contain less duplication code?