0
interface IA {
   B Foo();
}


interface IB {
   // more code
}

var a = Substitute.For<IA>();
var b = Substitute.For<IB>();

a.Foo().Returns(b);

Is that possible?

Daniel Gartmann
  • 11,678
  • 12
  • 45
  • 60
  • is this helpful in any way? http://nsubstitute.github.io/help/partial-subs/ been a while since i used nsubstitute, also just in case, read all of it as there is a good chance you will run REAL CODE! – user3012759 Aug 22 '14 at 13:38
  • Inside `interface IA` did you mean to have `IB Foo();` instead of `B Foo();`? – hunch_hunch Aug 22 '14 at 18:17

1 Answers1

1

Assuming that your interface IA is meant to have IB Foo(); and not B Foo();, yes, it's possible. See the following example, which uses NUnit.

namespace NSubstituteTests
{
    using NSubstitute;

    using NUnit.Framework;

    /// <summary>
    /// Corresponds to your type IA
    /// </summary>
    public interface IMyInterface
    {
        IMyOtherInterface Foo();
    }

    /// <summary>
    /// Corresponds to your type IB
    /// </summary>
    public interface IMyOtherInterface
    {
        string Message { get; }
    }

    [TestFixture]
    public class NSubstituteTest
    {
        [TestCase]
        public void TestSomething()
        {
            // var a = Substitute.For<IA>();
            var myConcreteClass = Substitute.For<IMyInterface>();

            // var b = Substitute.For<IB>();
            var myOtherConcreteClass = Substitute.For<IMyOtherInterface>();

            // a.Foo().Returns(b);
            myConcreteClass.Foo().Returns(myOtherConcreteClass);

            myOtherConcreteClass.Message.Returns("Thanks for testing!");

            var testResult = myConcreteClass.Foo().Message;
            Assert.AreEqual("Thanks for testing!", testResult);
        }
    }
}
hunch_hunch
  • 2,283
  • 1
  • 21
  • 26