Last active
November 19, 2021 02:44
-
-
Save Ganeshcse/b34ef561f4de24ae697c47b10f003abc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This utility is build using Nunit framework like other NUnit tests. | |
// My main goal of this utility is, | |
// if there is any exceptions then I have to fail my unit test. | |
// for any if conditions, instead of using a if...else, I have to fail my unit test. | |
// To achieve this, I am using Assert.Fail where ever necessary. Like few sample'e are below. | |
//if(value == null) | |
//{ | |
//Assert.Fail("Value cannot be null in this case"); | |
// I don't want to handle the code if the value is not null, instead | |
// I wanted it to fail my test. So far all good till here. | |
// Real problem I have explained it in the Step class. See below. | |
//} | |
class Session | |
{ | |
private List<UseCase> useCaseList; | |
public void ExecuteSession() | |
{ | |
foreach(var useCase in useCaseList) | |
{ | |
useCase.ExecuteUseCase(); | |
} | |
} | |
} | |
class UseCase | |
{ | |
private List<Step> stepList; | |
public void ExecuteUseCase() | |
{ | |
var step = stepList[0]; | |
step.ExecuteStep(); | |
} | |
} | |
class Step | |
{ | |
private IServiceModel serviceModel; | |
public Step() | |
{ | |
serviceModel = new ServiceModel(); | |
serviceModel.SomeEventInModel += OnSomeEventInModelFired; | |
} | |
// Event handler | |
private void OnSomeEventInModelFired(object sender, SomeEventInModelEventArgs args) | |
{ | |
try | |
{ | |
// Do some work and if any exception comes in this event handler. Catch block will catch it. | |
} | |
catch(Exception ex) | |
{ | |
// If I call Assert.Fail(), internally Assert.Fail method throws FailedException. | |
// Throwing exceptions from an event handler, causes the exception to propogate to the caller. | |
// and my control never comes back to my unit test. | |
// This is causing me a big problem. Now I wanted to know how to handle these kind of situations? | |
// If I don't assert it here then my test thinks all are good and then will try to execute further code which I don't want. | |
// Any thoughts on how to achieve this? | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment