|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 使用`TDD`方式。
-
- ```
- public class MyPrimeService
- {
- public bool IsPrime(int num)
- {
- throw new NotImplementedException("not implemented");
- }
- }
- ```
-
- 单元测试
-
- ```
- [Fact]
- public void IsPrime_InputIs1_ReturnFalse()
- {
- //Arrange
- var primeService = new MyPrimeService();
-
- //Act
- bool result = primeService.IsPrime(1);
-
- //Assert
- Assert.False(result, "1 should not be prime");
- }
- ```
-
- 修改代码让单元测试通过。
-
- ```
- public bool IsPrime(int num)
- {
- if(num==1)
- {
- return false;
- }
- throw new NotImplementedException("not implemented");
- }
- ```
-
- 目前单元测试输入条件有限,输入更多条件。
-
- ```
- [Theory]
- [InlineData(-1)]
- [InlineData(0)]
- [InlineData(1)]
- public void IsPrime_ValueLessThanTwo_ReturnFalse(int value)
- {
- //Arrange
- var primeService = new PrimeService.MyPrimeService();
-
- //Act
- var restul = primeService.IsPrime(value);
-
- //Assert
- Assert.False(restul, $"{value} should not be prime");
- }
- ```
-
- 有两个不通过
-
- ```
- 失败! - Failed: 2, Passed: 1, Skipped: 0, Total: 3, Duration: 4 ms
- ```
-
- 其实所有都不应该通过。修改代码:
-
- ```
- public bool IsPrime(int num)
- {
- if(num < 2)
- {
- return false;
- }
- throw new NotImplementedException("not implemented");
- }
- ```
-
-
-
|