我如何在 NSubstitute 中模拟 属性 setter
How can I mock a property setter in NSubstitute
这是我尝试做的事情:
Public Interface IDAO(Of T) : Inherits IDAO
Default Property Value(i As ULong) As T
Function Append(ts As ICollection(Of T)) As ULong
Function Remove(f As Predicate(Of T)) As ICollection(Of T)
Function GetRestricted(f As Predicate(Of T)) As ICollection(Of T)
Function RemoveFirst(f As Predicate(Of T)) As T
Function GetFirst(f As Predicate(Of T)) As T
End Interface
'...
_mock.WhenForAnyArgs(
Function(mock As IDAO(Of T)) mock.Value(Arg.Any(Of ULong)) = Arg.Any(Of T)()
).Do(
Sub(c As Core.CallInfo) Exit Sub 'Value(i) ... Set(v) ... _inner.data(i) = v
)
模式选择器Function(mock As IDAO(Of T)) mock.Value(Arg.Any(Of ULong)) = Arg.Any(Of T)()
不起作用;即使是这样,我也不知道如何在 Do
语句中使用 CallInfo
对象。
我不确定如何在 VB 中执行此操作,但这里是如何在 C# 中执行此操作,希望您或其他人可以翻译。
public interface IDao
{
ulong Value { get; set; }
}
[SuppressMessage("Argument specification", "NS3002:Can not find an argument to this call.", Justification = "Used in property set")]
[Fact]
public void PropertySetExample() {
var sub = Substitute.For<IDao>();
var value = 0ul;
sub.WhenForAnyArgs(x => x.Value = 0ul).Do(callInfo =>
// This will be called when sub.Value is set.
// For this example we just assign to a local variable.
value = callInfo.Arg<ulong>()
);
sub.Value = 42;
Assert.Equal(42ul, value);
}
(只有 NSubstitute.Analyzers 才需要 SuppressMessage
,因为它不确定 属性 集合的参数。)
这是我尝试做的事情:
Public Interface IDAO(Of T) : Inherits IDAO
Default Property Value(i As ULong) As T
Function Append(ts As ICollection(Of T)) As ULong
Function Remove(f As Predicate(Of T)) As ICollection(Of T)
Function GetRestricted(f As Predicate(Of T)) As ICollection(Of T)
Function RemoveFirst(f As Predicate(Of T)) As T
Function GetFirst(f As Predicate(Of T)) As T
End Interface
'...
_mock.WhenForAnyArgs(
Function(mock As IDAO(Of T)) mock.Value(Arg.Any(Of ULong)) = Arg.Any(Of T)()
).Do(
Sub(c As Core.CallInfo) Exit Sub 'Value(i) ... Set(v) ... _inner.data(i) = v
)
模式选择器Function(mock As IDAO(Of T)) mock.Value(Arg.Any(Of ULong)) = Arg.Any(Of T)()
不起作用;即使是这样,我也不知道如何在 Do
语句中使用 CallInfo
对象。
我不确定如何在 VB 中执行此操作,但这里是如何在 C# 中执行此操作,希望您或其他人可以翻译。
public interface IDao
{
ulong Value { get; set; }
}
[SuppressMessage("Argument specification", "NS3002:Can not find an argument to this call.", Justification = "Used in property set")]
[Fact]
public void PropertySetExample() {
var sub = Substitute.For<IDao>();
var value = 0ul;
sub.WhenForAnyArgs(x => x.Value = 0ul).Do(callInfo =>
// This will be called when sub.Value is set.
// For this example we just assign to a local variable.
value = callInfo.Arg<ulong>()
);
sub.Value = 42;
Assert.Equal(42ul, value);
}
(只有 NSubstitute.Analyzers 才需要 SuppressMessage
,因为它不确定 属性 集合的参数。)