Skip to content

Instantly share code, notes, and snippets.

@dtchepak

dtchepak/a.cs Secret

Last active February 5, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtchepak/3e561944b1cccd9238a1da8b34ffc9be to your computer and use it in GitHub Desktop.
Save dtchepak/3e561944b1cccd9238a1da8b34ffc9be to your computer and use it in GitHub Desktop.
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