/
RelaySimpleCommandTest.cs
112 lines (91 loc) 路 3.43 KB
/
RelaySimpleCommandTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
锘縰sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using FluentAssertions;
using System.Threading.Tasks;
using NSubstitute;
namespace MVVM.Component.Test
{
public class RelaySimpleCommandTest
{
[Fact]
public void RelaySimpleCommandShouldCallActionGeneric()
{
var action = Substitute.For<Action<object>>();
var target = new MVVM.Component.RelaySimpleCommand<object>(action);
var arg = new object();
target.Execute(arg);
action.Received(1).Invoke(arg);
}
[Fact]
public void RelaySimpleCommandShouldCallAction()
{
var action = Substitute.For<Action>();
var target = new MVVM.Component.RelaySimpleCommand(action);
var arg = new object();
target.Execute(arg);
action.Received(1).Invoke();
}
[Fact]
public void RelayResultCommandShouldCallFunctionGeneric()
{
var function = Substitute.For<Func<object,int>>();
var arg = new object();
function.Invoke(arg).Returns(122);
var target = new MVVM.Component.RelayResultCommand<object,int>(function);
var res = target.Execute(arg).Result;
function.Received(1).Invoke(arg);
res.Should().Be(122);
}
[Fact]
public void RelayResultCommandCreate_CreateCommandThatCallFunctionGeneric()
{
var function = Substitute.For<Func<object, int>>();
var arg = new object();
function.Invoke(arg).Returns(122);
var target = RelayResultCommand.Create(function);
var res = target.Execute(arg).Result;
function.Received(1).Invoke(arg);
res.Should().Be(122);
}
[Fact]
public void RelayResultCommandShouldHandleExceptionFunctionGeneric()
{
var exception = new Exception();
var function = Substitute.For<Func<object, int>>();
var arg = new object();
function.When(f => f.Invoke(arg)).Do(_ => { throw exception; });
var target = new MVVM.Component.RelayResultCommand<object, int>(function);
var res = target.Execute(arg);
function.Received(1).Invoke(arg);
res.IsFaulted.Should().BeTrue();
res.Exception.Flatten().InnerException.Should().Be(exception);
}
[Fact]
public void RelayResultCommandShouldCallFunctionGenericWithTask()
{
var function = Substitute.For<Func<object, Task<int>>>();
var tcs = new TaskCompletionSource<int>();
tcs.SetResult(35);
var arg = new object();
function.Invoke(arg).Returns(tcs.Task);
var target = new MVVM.Component.RelayResultCommand<object, int>(function);
var res = target.Execute(arg).Result;
function.Received(1).Invoke(arg);
res.Should().Be(35);
}
[Fact]
public void RelayResultCommandShouldCallFunction()
{
var function = Substitute.For<Func<int>>();
function.Invoke().Returns(12);
var target = MVVM.Component.RelayResultCommand.Create(function);
var arg = new object();
var res = target.Execute(arg).Result;
function.Received(1).Invoke();
res.Should().Be(12);
}
}
}