Пример #1
0
        static void Main(string[] args)
        {
            /*
             * Poor man's DI is just a way to keep your code SOLID without the use of a DI container
             */

            //for instance a typical developer might write a class like Foo where it's dependencies are trapped in the body of a method
            var foo = new Foo();

            foo.Blah();

            //in general, that may be good enough but if you inspect class Foo, you'll see that it needs class Bar. At test time
            //you'll have a difficult time mocking or switching off that code without doing some pretty strange configuration setups

            //meet class BetterFoo, it takes a dependency via the constructor
            var betterFoo = new BetterFoo(new Bar());

            betterFoo.Blah();

            //meet yet another twist on it by meeting class BetterFoo2. This class has a default use of IBar and doesn't need to be passed
            //explicitly AND it can be swapped out
            var betterFoo2 = new BetterFoo2();

            betterFoo2.Blah();

            //as-is, betterFoo2 will use Bar as the IBar impl, however if I want to use Bar2 instead, I can do the following
            betterFoo2.Bar = new Bar2();
            betterFoo2.Blah();
        }
Пример #2
0
        public void GivenCallThrough_ThenTheResultShouldBeSaved()
        {
            var obj   = new Foo();
            var mock  = new CacheMock(null, false).AsMock();
            var cache = new Cache <Foo>(() => obj.Blah(), mock.Object);

            cache.Fetch();

            mock.Verify(x => x.Write(obj), Times.Once);
        }
Пример #3
0
        public void GivenDirtyFile_ThenItShouldRunTheFunction()
        {
            var obj   = new Foo();
            var mock  = new CacheMock(obj, true).AsMock();
            var cache = new Cache <Foo>(() => obj.Blah(), mock.Object);

            cache.Fetch();

            Assert.That(obj.WasCalled, Is.True);
        }
Пример #4
0
 public void TestFoo()
 {
     Assert.AreEqual(Foo.Blah(), 1);
 }