Khi chế giễu một dịch vụ, đôi khi bạn muốn biết một phương thức trên dịch vụ đó đã được gọi bao nhiêu lần, tôi đã từng làm điều này trong quá khứ và biết về Verify
. Lớp này có một số phương thức tĩnh cho phép bạn kiểm tra điều này theo nhiều cách khác nhau
Một tùy chọn khác là sử dụng phương pháp CallBack
để tăng bộ đếm theo cách thủ công, sau đó kiểm tra xem bộ đếm có giá trị mong đợi với một Assert
Phương thức gọi để kiểm tra
Tôi có một thư viện rất đơn giản với một
1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
0 lấy một 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
1 làm tham số hàm tạo. Việc triển khai 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
1 không thành vấn đề, điều tôi muốn kiểm tra là số lần phương thức 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
3 được gọiSử dụng Xác minh
Đây có lẽ là cách tốt nhất để thực hiện vì Verify
được thiết kế cho mục đích chính xác này - để xác minh số lần một phương thức đã được gọi
Sắp xếp - Bên trong phương thức thử nghiệm, giả lập
1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
1 [dòng 5], thiết lập phương thức 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
3 để trả về phản hồi đã biết cho một lời gọi cụ thể [dòng 6], tạo 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
0 chuyển nó sang 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
3 đã chế nhạo [dòng 8]Hành động - Gọi phương thức công khai trên
1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
0. Điều này sẽ lần lượt gọi phương thức 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
3 một lần trên 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
3 bị chế giễu [dòng 11]Khẳng định - Cuối cùng khẳng định rằng
1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
3 đã được gọi một lần [dòng 15] 1[Fact]
2public void Check_Times_Called_With_Verify[]
3{
4 // Arrange
5 Mock mathService = new Mock[];
6 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3];
7
8 FooService fooService = new FooService[mathService.Object];
9
10 // Act
11 string result1 = fooService.AddMessageToAnswer["hello", 1, 2];
12
13 // Assert
14 Assert.Equal["hello 3", result1];
15 mathService.Verify[ms => ms.AddNumbers[1, 2], Times.Exactly[1]];
16}
Sử dụng gọi lại
Một cách tiếp cận khác là sử dụng
1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
8 tích hợp có thể thực thi trên từng phương thức là 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
9 trên một dịch vụ giả lập. Cú pháp của nó rất đơn giản, thêm 1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
8 sau Verify
1. Trong ví dụ này, tôi sử dụng nó để tăng bộ đếm [dòng 6]Phần còn lại của bài kiểm tra giống với Assert, nơi tôi kiểm tra bộ đếm đó có giá trị mong đợi không [dòng 16]
1[Fact]
2public void Check_Times_Called_With_Callback[]
3{
4 // Arrange
5 int timesCalled = 0;
6 Mock mathService = new Mock[];
7 mathService.Setup[ms => ms.AddNumbers[1, 2]].Returns[3].Callback[[] => ++timesCalled];
8
9 FooService fooService = new FooService[mathService.Object];
10
11 // Act
12 string result = fooService.AddMessageToAnswer["hello", 1, 2];
13
14 // Assert
15 Assert.Equal["hello 3", result];
16 Assert.Equal[1, timesCalled];
17}
Nó. Là Bất kỳ
Cả hai cách tiếp cận đều hoạt động với Verify
2 đối với các tham số của phương thức giả định hoặc khi xác nhận với Verify
. Tệp zip đính kèm có các ví dụ về điều này