Исходный код вики NSubstitute

Редактировал(а) Alexandr Fokin 2026/03/17 15:36

Последние авторы
1 |(% style="width:131px" %) |(% style="width:1351px" %)(((
2 | |nsubstitute/NSubstitute
3 [[https:~~/~~/github.com/nsubstitute/NSubstitute>>https://github.com/nsubstitute/NSubstitute]]
4 | |Docs
5 [[https:~~/~~/nsubstitute.github.io/help/getting-started/index.html>>https://nsubstitute.github.io/help/getting-started/index.html]]
6 | |
7 )))
8 |(% style="width:131px" %)Преимущества|(% style="width:1351px" %)(((
9 |[[Moq>>doc:Разработка.NET.Библиотеки.Тестирование.Moq.WebHome]] для создания заглушки используется билдер, который формирует экземпляр. NSubstitute позволяет редактировать заглушку в любой момент (а не только в на этапе конфигурации), иногда это может быть удобно.|
10 |NSubstitute позволяет указывать тип заглушки не только через Generic, но и через [[Reflection ~~~| Рефлексия>>doc:Разработка.NET.C#.Рантайм и типы.Reflection | Рефлексия .WebHome]] указывая {{code language="c#"}}Type{{/code}} интерфейса.
11 Также есть возможность указать метод через {{code language="c#"}}MethodInfo{{/code}}.|
12 | |
13 )))
14 |(% style="width:131px" %)Сценарии|(% style="width:1351px" %)(((
15 |(% style="width:236px" %)Регистрация обработчиков|(% style="width:1096px" %){{code language="c#"}}var stub = Substitute.For<IService>();
16 // хендлер метода без возврата значения.
17 stub
18 .When(e => e.Action1(Arg.Any<string>()))
19 .Do(e => { });
20 // хендлер, возвращающий результат
21 stub.Action1(Arg.Any<string>())
22 .Returns(e => "test");{{/code}}
23 |(% style="width:236px" %)Создание заглушки по Type|(% style="width:1096px" %){{code language="c#"}}var stub = Substitude.For(typeof(IService), []);{{/code}}
24 |(% style="width:236px" %)Регистрация обработчика по MethodInfo|(% style="width:1096px" %)(((
25 Can NSubstitute mock the return of a MethodInfo?
26 [[https:~~/~~/stackoverflow.com/questions/41918308/can-nsubstitute-mock-the-return-of-a-methodinfo>>https://stackoverflow.com/questions/41918308/can-nsubstitute-mock-the-return-of-a-methodinfo]]
27
28 {{code language="c#"}}
29 var stub = Substitute.For<IService>();
30 var methodInfo = typeof(IService).GetMethod("Method1");
31 methodInfo.Invoke(stub, null).Returns((e) => "stub");
32
33 {{/code}}
34 )))
35 |(% style="width:236px" %) |(% style="width:1096px" %)
36 )))
37
38
39
40 ----
41
42 ==== Внутренние ссылки: ====
43
44 ====== Дочерние страницы: ======
45
46 {{children/}}
47
48 ====== Обратные ссылки: ======
49
50 {{velocity}}
51 #set ($links = $doc.getBacklinks())
52 #if ($links.size() > 0)
53 #foreach ($docname in $links)
54 #set ($rdoc = $xwiki.getDocument($docname).getTranslatedDocument())
55 * [[$escapetool.xml($rdoc.fullName)]]
56 #end
57 #else
58 No back links for this page!
59 #end
60 {{/velocity}}
61
62 ----