-
-
Save dtchepak/3e561944b1cccd9238a1da8b34ffc9be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xunit; | |
using NSubstitute; | |
namespace NSubWorkshop | |
{ | |
public interface IC { | |
int GetInt(string s); | |
} | |
public interface IB { | |
string GetString(string s); | |
} | |
public interface IA : IB, IC { } | |
public class BImpl : IB { | |
public virtual string GetString(string s) { | |
return $"{s}!"; | |
} | |
} | |
public class UnitTest1 | |
{ | |
[Fact] | |
public void Partial() { | |
var sub = Substitute.For<IA, BImpl>(); | |
// Method on IB / BImpl should call base implementation: | |
sub.When(x => x.GetString(Arg.Any<string>())).CallBase(); | |
// Stub method on IC: | |
sub.GetInt("hi").Returns(42); | |
Assert.Equal("hi!", sub.GetString("hi")); | |
Assert.Equal(42, sub.GetInt("hi")); | |
} | |
[Fact] | |
public void ForwardSpecificCalls() { | |
var b = new BImpl(); | |
var sub = Substitute.For<IA>(); | |
sub.GetString("").ReturnsForAnyArgs(x => b.GetString(x.Arg<string>())); | |
sub.GetInt("hi").Returns(42); | |
Assert.Equal("hi!", sub.GetString("hi")); | |
Assert.Equal(42, sub.GetInt("hi")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment