Example #1
0
        public void TestTheWholeThingBecauseWeCan()
        {
            // This noise will be in every one of my tests.
            // I can tuck it somewhere common, but we can also get rid of it entirely.
            var log     = new MockLog(); // or new Mock<ILog>().Error((r) => true)... sucks either way.
            var db      = new MockDb();  // there's undoubtedly more dependencies to be created here.
            var smtp    = new MockSmtp();
            var myClass = new MyClass(log, db, smtp);

            // All of that setup, just to get to here
            var request = new Request(1, "Yossarian", "*****@*****.**");
            var actual  = myClass.MyMethod(request);

            // MyMethod is the only testable unit here.
            // All of this setup is required for every test.
            // I can't test the validation logic independently.

            Assert.AreEqual("Ok", actual);
        }
        public void TestTheWholeThingBecauseWeCan()
        {
            // Still noisy
            var log     = new MockLog();
            var db      = new MockDb();
            var smtp    = new MockSmtp();
            var myClass = new MyClass(log, db, smtp);
            var request = new Request(1, "Yossarian", "*****@*****.**");

            // I made my UpdateDatabase call internal so I could test it.
            // I don't want production callers to do this, because it's not what my class is for.
            // class changes made for testing is an unacceptable compromise!
            // I also still had to string up ALL of the class dependencies just to test this.
            // This function doesn't even use a logger.
            // Passing a null logger into the constructor might work today, but possibly not tomorrow (brittle tests).
            // But still... I don't give a damn about that smtp thing.  But i have to make one every time.
            // Dependency management digression here ---> ...
            var actual = myClass.UpdateDatabase(request);

            Assert.AreEqual("Ok", actual);
        }