markvincze / Stubbery

Library for creating Api stubs in .NET. https://markvincze.github.io/Stubbery/
MIT License
74 stars 14 forks source link

Check if ApiStub state #37

Closed donaldgray closed 1 year ago

donaldgray commented 1 year ago

Is it possible to check the ApiStub state, prior to starting? I have an extension method that handles this but it feels a little rough:

try
{
    apiStub.Start();
}
catch (InvalidOperationException ex) when (ex.Message == "The api stub is already started.")
{
}

but I would much prefer if it was possible to safely start (apiStub.TryStart()) or have some way to check the internal ApiStubState value - am I missing something?

The reason I'm looking for this is that it will simplify my xunit test setup as I use a IClassFixture<ApiStub> to control the lifecycle of the ApiStub but I need to call .Start() when on first run.

markvincze commented 1 year ago

Hi @donaldgray,

I wasn't sure from the top of my head, it's been a while since I worked on this project 🙂 I don't think there is any other way at the moment besides the workaround you posted. But it shouldn't be difficult to either make the ApiStub.state value public so that you can check it, or create a TryStart() variant as you suggested. I'll try to get around to implement one of these improvements in the coming days.

markvincze commented 1 year ago

@donaldgray I released version 2.8.3, which includes the EnsureStarted() method: http://markvincze.github.io/Stubbery/api/Stubbery.ApiStub.html#Stubbery_ApiStub_EnsureStarted, can you check if this solves your scenario?

donaldgray commented 1 year ago

@markvincze - tested and it's working as expected, thank you!