Skip to content

Poser ignoring early return statement #61

@mai-pai

Description

@mai-pai

Hi, I'm seeing some weird behavior where an early return is ignored and the code continues to execute. For the following code, execution should have returned if the Id of input is less than 1. However, the code below the return statement still executes.

public ServiceOutput Create(ServiceInput input)
{
  var serviceOutput = new ServiceOutput();
  try
  {
    LegacyClass.FirstStaticMethodThatThrowsException();
    if (input.Id < 1)
    {
      serviceOutput.SetStatus_Failure("Id must be set");
      return serviceOutput;
    }
    serviceOutput.Email = input.Email?.Trim() ?? string.Empty;
    ClassWithStaticMethod.SecondStaticMethodThatThrowsException();
  }
  catch (Exception ex)
  {
    serviceOutput.SetStatus_Failure(ex);
  }
  finally
  {
    if (serviceOutput._OperationStatus != 0)
    {
      ErrorHandler.PublishError(serviceOutput._ExtendedDescription);
    }
    LegacyLogger.WriteLogInfo(serviceOutput._ExtendedDescription);
  }
  return serviceOutput;
}

The following is the UnitTest for the above method. secondStaticMethodCalled should still be false but the test fails since it's true because the second shim is called.

[Fact]
public void PoseBugReproductionTests()
{
  var secondStaticMethodCalled = false;
  var shim1 = Shim
    .Replace(() => LegacyClass.FirstStaticMethodThatThrowsException())
    .With(delegate () { });

  var shim2 = Shim
    .Replace(() => ClassWithStaticMethod.SecondStaticMethodThatThrowsException())
    .With(delegate ()
    {
      secondStaticMethodCalled = true;
    });

  var outObj = default(ServiceOutput);
  PoseContext.Isolate(() =>
  {
    var inputObj = new ServiceInput
    {
      Email = "user@example.com",
      Id = 0
    };

    var service = new ServiceUnderTest();
    outObj = service.Create(inputObj);
  }, shim1, shim2);

  Assert.NotNull(outObj);
  Assert.Equal("Id must be set", outObj._ExtendedDescription);
  Assert.Equal(-2, outObj._OperationStatus);
  Assert.False(secondStaticMethodCalled);
}

A full solution/project that replicates the issue is at https://github.com/mai-pai/poser-bug-reproduction.
The project is using .NET 8 and is using Poser verion 2.1.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpriority: highThe issue has high priority

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions