csharp / beginner
Snippet
Component Behavior Validation
Testing logic ensures your code works as expected. In this beginner example, we manually verify the output of a function and throw an error if it fails.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class SimpleCalculator{public int Add(int a, int b) => a + b;}public class LogicTester{public void RunTest(){var calc = new SimpleCalculator();int result = calc.Add(10, 5);// Basic manual assertion logicif (result != 15){throw new System.Exception($"Test Failed: Expected 15, got {result}");}}}
Breakdown
1
int result = calc.Add(10, 5);
Calls the method to be tested.
2
if (result != 15)
Compares the actual result against the expected value.