How do you unit test an interface?

后端 未结 5 1705
忘掉有多难
忘掉有多难 2020-12-13 13:37

For example, there is a interface IMyInterface, and three classes support this interface:

class A : IMyInterface
{
}

class B : IMyInterface
{
}         


        
5条回答
  •  悲&欢浪女
    2020-12-13 14:31

    If you're using NUnit, then you could use Grensesnitt:

    public interface ICanAdd {
        int Add(int i, int j); //dont ask me why you want different adders
    }
    
    public class winefoo : ICanAdd {
        public int Add(int i, int j)
        {
            return i + j;
        }
    }
    
    interface winebar : ICanAdd {
        void FooBar() ; 
    }
    
    public class Adder1 : winebar {
        public int Add(int i, int j) {
            return i + j;
        } 
        public void FooBar() {}
    }
    
    public class Adder2 : ICanAdd {
        public int Add(int i, int j) {
            return (i + 12) + (j - 12 ); //yeeeeeaaaah
        } 
    }
    
    [InterfaceSpecification]
    public class WithOtherPlugins : AppliesToAll
    { 
        [TestCase(1, 2, 3)] 
        [TestCase(-1, 2, 1)]
        [TestCase(0, 0, 0)]
        public void CanAddOrSomething(int x, int y, int r)
        {
            Assert.AreEqual(subject.Add(x, y), r);
        }
    
        [TestCase(1, 2, Result = 3)]
        [TestCase(-1, 2, Result = 1)]
        [TestCase(0, 0, Result = 0)]
        public int CannAddOrSomethingWithReturn(int x, int y) {
            return subject.Add(x, y);
        }
    }
    

提交回复
热议问题