#111 Fix Arg.Any matches for ref and out params
This commit is contained in:
Родитель
bd80dd2d81
Коммит
0432d20a54
|
@ -120,6 +120,7 @@
|
|||
<Compile Include="SubbingForConcreteTypesAndMultipleInterfaces.cs" />
|
||||
<Compile Include="SubstituteTimingAndInteractions.cs" />
|
||||
<Compile Include="Infrastructure\Task.cs" />
|
||||
<Compile Include="TestRefAndOutArgAnyWithWhenDo.cs" />
|
||||
<Compile Include="WhenCalledDo.cs" />
|
||||
<Compile Include="SubstitutingForDelegates.cs" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -0,0 +1,101 @@
|
|||
using NUnit.Framework;
|
||||
|
||||
namespace NSubstitute.Acceptance.Specs
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestRefAndOutArgAnyWithWhenDo
|
||||
{
|
||||
[Test]
|
||||
public void WhenDo_GivenRefParamWithArgAnyString_ShouldCallDo()
|
||||
{
|
||||
var substitute = Substitute.For<IDummy>();
|
||||
var p = new DummyLauncher();
|
||||
var counter = 0;
|
||||
var strArg = Arg.Any<string>();
|
||||
substitute.When(x => x.SomeMethodWithARefArg(ref strArg)).Do(x => counter++);
|
||||
p.LaunchTheRefDummy(substitute);
|
||||
Assert.That(counter, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenDo_GivenRefParamWithActualString_ShouldCallDo()
|
||||
{
|
||||
var substitute = Substitute.For<IDummy>();
|
||||
var p = new DummyLauncher();
|
||||
var counter = 0;
|
||||
var strArg = "hello";
|
||||
substitute.When(x => x.SomeMethodWithARefArg(ref strArg)).Do(x => counter++);
|
||||
p.LaunchTheRefDummy(substitute);
|
||||
Assert.That(counter, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenDo_GivenRefParamWithDifferentString_ShouldNotCallDo()
|
||||
{
|
||||
var substitute = Substitute.For<IDummy>();
|
||||
var p = new DummyLauncher();
|
||||
var counter = 0;
|
||||
var strArg = "what";
|
||||
substitute.When(x => x.SomeMethodWithARefArg(ref strArg)).Do(x => counter++);
|
||||
p.LaunchTheRefDummy(substitute);
|
||||
Assert.That(counter, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenDo_GivenOutParamWithArgAnyString_ShouldCallDo()
|
||||
{
|
||||
var substitute = Substitute.For<IDummy>();
|
||||
var p = new DummyLauncher();
|
||||
var counter = 0;
|
||||
var strArg = Arg.Any<string>();
|
||||
substitute.When(x => x.SomeMethodWithAnOutArg(out strArg)).Do(x => counter++);
|
||||
p.LaunchTheOutDummy(substitute);
|
||||
Assert.That(counter, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenDo_GivenOutParamWithActualString_ShouldCallDo()
|
||||
{
|
||||
var substitute = Substitute.For<IDummy>();
|
||||
var p = new DummyLauncher();
|
||||
var counter = 0;
|
||||
var strArg = "hello";
|
||||
substitute.When(x => x.SomeMethodWithAnOutArg(out strArg)).Do(x => counter++);
|
||||
p.LaunchTheOutDummy(substitute);
|
||||
Assert.That(counter, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenDo_GivenOutParamWithDifferentString_ShouldNotCallDo()
|
||||
{
|
||||
var substitute = Substitute.For<IDummy>();
|
||||
var p = new DummyLauncher();
|
||||
var counter = 0;
|
||||
var strArg = "";
|
||||
substitute.When(x => x.SomeMethodWithAnOutArg(out strArg)).Do(x => counter++);
|
||||
p.LaunchTheOutDummy(substitute);
|
||||
Assert.That(counter, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
public class DummyLauncher
|
||||
{
|
||||
public void LaunchTheRefDummy(IDummy dummy)
|
||||
{
|
||||
var test = "hello";
|
||||
dummy.SomeMethodWithARefArg(ref test);
|
||||
}
|
||||
|
||||
public void LaunchTheOutDummy(IDummy dummy)
|
||||
{
|
||||
var test = "hello";
|
||||
dummy.SomeMethodWithAnOutArg(out test);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IDummy
|
||||
{
|
||||
bool SomeMethodWithARefArg(ref string str);
|
||||
bool SomeMethodWithAnOutArg(out string str);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,7 +54,8 @@ namespace NSubstitute.Core.Arguments
|
|||
|
||||
private bool AreTypesCompatible(Type argumentType, Type typeArgSpecIsFor)
|
||||
{
|
||||
return argumentType.IsAssignableFrom(typeArgSpecIsFor);
|
||||
return argumentType.IsAssignableFrom(typeArgSpecIsFor) ||
|
||||
(argumentType.IsByRef && !typeArgSpecIsFor.IsByRef && argumentType.IsAssignableFrom(typeArgSpecIsFor.MakeByRefType()));
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче