Exemplo n.º 1
0
        public OverloadSpec()
        {
            Describe("Method with overloads", () =>
            {
                It("should have all overloads covered by the one spy.", () =>
                {
                    var spy = Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.PositiveOne)).And.ReturnValues(3, 2, 1);

                    Expect(TestSubject.PositiveOne()).ToBe(3);
                    Expect(TestSubject.PositiveOne(0)).ToBe(2);
                    Expect(TestSubject.PositiveOne(0)).ToBe(1);
                });

                It("should record calls to both overloads.", () =>
                {
                    var spy = Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Write));

                    TestSubject.Write(1, 2, 3);
                    TestSubject.Write(3);
                    TestSubject.Write(2, 3);

                    Expect(spy).ToHaveBeenCalledWith(2, 3);
                    Expect(spy).ToHaveBeenCalledWith(3);
                    Expect(spy).ToHaveBeenCalledWith(1, 2, 3);
                });
            });
        }
Exemplo n.º 2
0
        public AdderSpec()
        {
            Describe <Adder>(() =>
            {
                It("can add a positive number to a negative number", () =>
                {
                    var adder = new Adder();
                    Expect(adder.Add(-1, 1)).ToEqual(0M);
                });
            });

            Describe <Adder>(() =>
            {
                List <decimal[]> specs = new List <decimal[]>
                {        // n1, n2, expected
                    new[] { -7M, 3M, -4M },
                    new[] { 0M, 0M, 0M },
                    new[] { 999999999M, 1M, 1000000000M }
                };

                foreach (var spec in specs)
                {
                    It($"can add 2 numbers ({spec[0]}, {spec[1]})", () =>
                    {
                        var adder = new Adder();
                        Expect(adder.Add(spec[0], spec[1])).ToEqual(spec[2]);
                    });
                }
            });

            Describe <Adder>(() =>
            {
                It("appends to history", () =>
                {
                    OperationHistory history = new OperationHistory();
                    Spy historySpy           = Jaz.SpyOn(history, nameof(history.Append));
                    var adder = new Adder(history);

                    adder.Add(-1, 1);

                    Expect(historySpy).ToHaveBeenCalled();
                });
            });

            Describe <Adder>(() =>
            {
                Describe(nameof(Adder.Add), () =>
                {
                    It("can add a positive number to a negative number", () =>
                    {
                        var adder = new Adder();
                        Expect(adder.Add(-1, 1)).ToEqual(0M);
                    });
                });
            });
        }
Exemplo n.º 3
0
        public ExternSpec()
        {
            Describe("Extern methods", () =>
            {
                It("should call through by default.", () =>
                {
                    Expect(GetInt()).ToBe(50);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(typeof(ExternSpec), nameof(GetInt));
                    Expect(GetInt()).ToBeDefault();
                });

                It("should return the configured value.", () =>
                {
                    Jaz.SpyOn(typeof(ExternSpec), nameof(GetInt)).And.ReturnValue(1);
                    var result = GetInt();
                    Expect(result).ToBe(1);
                });

                It("should return the configured values in sequence.", () =>
                {
                    Jaz.SpyOn(typeof(ExternSpec), nameof(GetInt)).And.ReturnValues(1, 2, 3, 4);
                    var result = GetInt();
                    Expect(result).ToBe(1);
                    result = GetInt();
                    Expect(result).ToBe(2);
                    result = GetInt();
                    Expect(result).ToBe(3);
                    result = GetInt();
                    Expect(result).ToBe(4);
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(typeof(ExternSpec), nameof(GetInt)).And.CallThrough();
                    var result = GetInt();
                    Expect(result).ToBe(50);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(typeof(ExternSpec), nameof(GetInt)).And.Throw(exception);
                    var thrown = Expect(() => GetInt()).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });
            });
        }
Exemplo n.º 4
0
        public InterfaceSpec()
        {
            Describe("Interface", () =>
            {
                Describe("implementations", () =>
                {
                    It("should call spy wrapper on method calls.", () =>
                    {
                        var action      = (Action)(() => { });
                        var testSubject = (ITestSubject) new TestSubject();

                        var actionSpy = Jaz.SpyOn(action, nameof(action.Invoke));

                        testSubject.Call(action);

                        Expect(actionSpy).ToHaveBeenCalled();
                    });
                });
            });
        }
Exemplo n.º 5
0
        public VirtualMethodSpec()
        {
            Describe("Virtual Method", () =>
            {
                Describe("overrides", () =>
                {
                    It("should call spy wrapper on method calls.", () =>
                    {
                        var action      = (Action)(() => { });
                        var testSubject = (TestSubjectBase) new TestSubject();

                        var actionSpy = Jaz.SpyOn(action, nameof(action.Invoke));

                        testSubject.Call(action);

                        Expect(actionSpy).ToHaveBeenCalled();
                    });
                });
            });
        }
Exemplo n.º 6
0
        public YieldReturnSpec()
        {
            Describe("Method using yield return", () =>
            {
                It("should call through by default.", () =>
                {
                    var result = TestSubject.Method().ToList();
                    Expect(result).ToEqual(new[] { 1, 2, 3, 5, 7 });
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Method));
                    var result = TestSubject.Method();
                    Expect(result).ToBeDefault();
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Method)).And.CallThrough();
                    var result = TestSubject.Method().ToList();
                    Expect(result).ToEqual(new[] { 1, 2, 3, 5, 7 });
                });

                It("should throw an exception as configured.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Method)).And.Throw <TestException>();
                    Expect(() => TestSubject.Method()).ToThrow <TestException>();
                });

                It("should return the configured value.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Method)).And.ReturnValue(new[] { 4, 6, 8, 9 });
                    var result = TestSubject.Method().ToList();
                    Expect(result).ToEqual(new[] { 4, 6, 8, 9 });
                });
            });
        }
Exemplo n.º 7
0
        public CallsSpec()
        {
            Describe("Calls", () =>
            {
                TestSubject testSubject = null;

                BeforeEach(() =>
                {
                    testSubject = new TestSubject();
                });

                It("should be empty for a new spy.", () =>
                {
                    var spy = Jaz.SpyOn(testSubject, nameof(testSubject.Action));
                    Expect(spy.Calls).ToBeEmpty();
                });

                It("should contain an ordered call log.", () =>
                {
                    var spy = Jaz.SpyOn(testSubject, nameof(testSubject.Action));
                    testSubject.Action(1, 2.0, "three");
                    testSubject.Action(2, 1.0, null);

                    Expect(spy.Calls).ToEqual(new[]
                    {
                        new
                        {
                            Arguments = new object[] { 1, 2.0, "three" }
                        },
                        new
                        {
                            Arguments = new object[] { 2, 1.0, null }
                        }
                    });
                });
            });
        }
Exemplo n.º 8
0
        public BehaviourSpec()
        {
            var testSubject = new TestSubject();

            Describe("Behaviour", () =>
            {
                Describe("of Return Value", () =>
                {
                    It("should return the provided return value.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue("abc");
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");
                    });

                    It("should cycle through return values once when using return values.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValues("a", "b", "c");
                        var result = testSubject.Func();
                        Expect(result).ToBe("a");
                        result = testSubject.Func();
                        Expect(result).ToBe("b");
                        result = testSubject.Func();
                        Expect(result).ToBe("c");
                    });
                });

                Describe("of Call Through", () =>
                {
                    It("should call through to the original implementation.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.CallThrough();
                        var result = testSubject.Func();
                        Expect(result).ToBe("xyz");
                    });
                });

                Describe("of Default", () =>
                {
                    It("should return the default value of the function.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func));
                        var result = testSubject.Func();
                        Expect(result).ToBeDefault();
                    });

                    It("should have no logic executed for an action.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Action));
                        testSubject.Action();
                        Expect(testSubject.Value).ToBeDefault();
                    });
                });

                Describe("of Throw", () =>
                {
                    It("should throw the given exception.", () =>
                    {
                        var exception = new System.Exception();
                        Jaz.SpyOn(testSubject, nameof(testSubject.Action)).And.Throw(exception);

                        try
                        {
                            testSubject.Action();
                            Fail("Expected an exception to be thrown.");
                        }
                        catch (Exception ex)
                        {
                            if (ex != exception)
                            {
                                throw;
                            }
                        }
                    });

                    It("should throw an exception of the given type.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Action)).And.Throw <Exception>();

                        try
                        {
                            testSubject.Action();
                            Fail("Expected an exception to be thrown.");
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message != "Exception of type 'System.Exception' was thrown.")
                            {
                                throw;
                            }
                        }
                    });
                });

                Describe("in sequence", () =>
                {
                    It("should execute forever if added using And.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue("abc");
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");
                    });

                    It("should clear existing behaviours if added using And.", () =>
                    {
                        var spy = Jaz.SpyOn(testSubject, nameof(testSubject.Func));
                        spy.And.Throw <Exception>();
                        spy.And.ReturnValue("abc");
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");
                    });

                    It("should execute once if added using Then.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue(null).Then.ReturnValue("abc");
                        var result = testSubject.Func();
                        Expect(result).ToBeDefault();
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");

                        try
                        {
                            testSubject.Func();
                            Fail("Expected spy exception after running out of behaviours.");
                        }
                        catch (JazSpyException)
                        {
                        }
                    });

                    It("should execute once if Once is specified.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue("abc").Once();
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");

                        try
                        {
                            testSubject.Func();
                            Fail("Expected spy exception after running out of behaviours.");
                        }
                        catch (JazSpyException)
                        {
                        }
                    });

                    It("should excute twice if Twice is specified.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue("abc").Twice();
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");

                        try
                        {
                            testSubject.Func();
                            Fail("Expected spy exception after running out of behaviours.");
                        }
                        catch (JazSpyException)
                        {
                        }
                    });

                    It("should execute the number of times specified in Times.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue("abc").Times(3);
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");

                        try
                        {
                            testSubject.Func();
                            Fail("Expected spy exception after running out of behaviours.");
                        }
                        catch (JazSpyException)
                        {
                        }
                    });

                    It("should execute forever if Forever is specified.", () =>
                    {
                        Jaz.SpyOn(testSubject, nameof(testSubject.Func)).And.ReturnValue("abc").Forever();
                        var result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");
                        result = testSubject.Func();
                        Expect(result).ToBe("abc");
                    });
                });
            });
        }
Exemplo n.º 9
0
        public ByRefSpec()
        {
            TestSubject testSubject = null;

            Describe("Action with by ref parameter", () =>
            {
                float testValue = default;

                BeforeEach(() =>
                {
                    testSubject = new TestSubject();
                    testValue   = 85.5f;
                });

                It("should call through by default.", () =>
                {
                    testSubject.PercentToDecimal(ref testValue);
                    Expect(testValue).ToBe(0.855f);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimal));
                    testSubject.PercentToDecimal(ref testValue);
                    Expect(testValue).ToBe(85.5f);
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimal)).And.CallThrough();
                    testSubject.PercentToDecimal(ref testValue);
                    Expect(testValue).ToBe(0.855f);
                });

                It("should throw an exception as configured.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimal)).And.Throw <TestException>();
                    Expect(() => testSubject.PercentToDecimal(ref testValue)).ToThrow <TestException>();
                });

                It("should change the ref parameter as configured.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimal)).And.DoNothing().ThenChangeParameter("value", 1.0f);
                    testSubject.PercentToDecimal(ref testValue);
                    Expect(testValue).ToBe(1.0f);
                });
            });

            Describe("Action with out parameter", () =>
            {
                float testValue = default;

                BeforeEach(() =>
                {
                    testSubject = new TestSubject();
                    testValue   = 85.5f;
                });

                It("should call through by default.", () =>
                {
                    testSubject.PercentToDecimalPreserve(testValue, out var output);
                    Expect(output).ToBe(0.855f);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimalPreserve));
                    testSubject.PercentToDecimalPreserve(testValue, out var output);
                    Expect(output).ToBeDefault();
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimalPreserve)).And.CallThrough();
                    testSubject.PercentToDecimalPreserve(testValue, out var output);
                    Expect(output).ToBe(0.855f);
                });

                It("should throw an exception as configured.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimalPreserve)).And.Throw <TestException>();
                    Expect(() => testSubject.PercentToDecimalPreserve(testValue, out var output)).ToThrow <TestException>();
                });

                It("should change the ref parameter as configured.", () =>
                {
                    Jaz.SpyOn(testSubject, nameof(testSubject.PercentToDecimalPreserve)).And.DoNothing().ThenChangeParameter("decimal", 1.0f);
                    testSubject.PercentToDecimalPreserve(testValue, out var output);
                    Expect(output).ToBe(1.0f);
                });
            });
Exemplo n.º 10
0
        public StaticMethodSpec()
        {
            Describe("A static action", () =>
            {
                BeforeEach(() =>
                {
                    TestSubject.Value = 0;
                });

                It("should call through by default.", () =>
                {
                    TestSubject.Iterate(3);
                    Expect(TestSubject.Value).ToBe(3);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Iterate));
                    TestSubject.Iterate(3);
                    Expect(TestSubject.Value).ToBeDefault();
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Iterate)).And.CallThrough();
                    TestSubject.Iterate(3);
                    Expect(TestSubject.Value).ToBe(3);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Iterate)).And.Throw(exception);
                    var thrown = Expect(() => TestSubject.Iterate(3)).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });

                // the only non-generic spy entry point
                Describe("with no parameters", () =>
                {
                    It("should call through by default.", () =>
                    {
                        TestSubject.Increment();
                        Expect(TestSubject.Value).ToBe(1);
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Increment));
                        TestSubject.Increment();
                        Expect(TestSubject.Value).ToBeDefault();
                    });

                    It("should call through as configured.", () =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Increment)).And.CallThrough();
                        TestSubject.Increment();
                        Expect(TestSubject.Value).ToBe(1);
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var exception = new TestException();
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Increment)).And.Throw(exception);
                        var thrown = Expect(() => TestSubject.Increment()).ToThrow <TestException>();
                        Expect(thrown).ToBe(exception);
                    });
                });
            });

            Describe("A static function", () =>
            {
                It("should call through by default.", () =>
                {
                    var result = TestSubject.Add5(3);
                    Expect(result).ToBe(8);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Add5));
                    var result = TestSubject.Add5(3);
                    Expect(result).ToBeDefault();
                });

                It("should return the configured value.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Add5)).And.ReturnValue(1);
                    var result = TestSubject.Add5(3);
                    Expect(result).ToBe(1);
                });

                It("should return the configured values in sequence.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Add5)).And.ReturnValues(1, 2, 3, 4);
                    var result = TestSubject.Add5(3);
                    Expect(result).ToBe(1);
                    result = TestSubject.Add5(3);
                    Expect(result).ToBe(2);
                    result = TestSubject.Add5(3);
                    Expect(result).ToBe(3);
                    result = TestSubject.Add5(3);
                    Expect(result).ToBe(4);
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Add5)).And.CallThrough();
                    var result = TestSubject.Add5(3);
                    Expect(result).ToBe(8);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.Add5)).And.Throw(exception);
                    var thrown = Expect(() => TestSubject.Add5(3)).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });
            });
        }
Exemplo n.º 11
0
        public SpyExpectSpec()
        {
            Describe <SpyExpect>(() =>
            {
                TestSubject testSubject = null;
                Spy spy = null;

                BeforeEach(() =>
                {
                    testSubject = new TestSubject();
                    spy         = Jaz.SpyOn(testSubject, nameof(testSubject.Action));
                });

                Describe(nameof(SpyExpect.Not), () =>
                {
                    It("should invert the check.", () =>
                    {
                        Expect(spy).Not.ToHaveBeenCalled();
                    });
                });

                Describe(nameof(SpyExpect.ToHaveBeenCalled), () =>
                {
                    It("should fail if the spy was not called.", () =>
                    {
                        Expect(() => Expect(spy).ToHaveBeenCalled()).ToThrow <JazExpectationException>();
                    });

                    It("should pass if the spy was called.", () =>
                    {
                        testSubject.Action(1, 2.0, "three");
                        Expect(spy).ToHaveBeenCalled();
                    });

                    Describe("with Not", () =>
                    {
                        It("should invert the check.", () =>
                        {
                            Expect(spy).Not.ToHaveBeenCalled();

                            testSubject.Action(1, 2.0, "three");
                            Expect(() => Expect(spy).Not.ToHaveBeenCalled()).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(SpyExpect.ToHaveBeenCalledTimes), () =>
                {
                    It("should fail if the spy was called less than the expected number of times.", () =>
                    {
                        Expect(() => Expect(spy).ToHaveBeenCalledTimes(1)).ToThrow <JazExpectationException>();
                    });

                    It("should pass if the spy was called the expected number of times.", () =>
                    {
                        testSubject.Action(1, 2.0, "three");
                        Expect(spy).ToHaveBeenCalledTimes(1);
                    });

                    It("should fail if the spy was called more than the expected number of times.", () =>
                    {
                        testSubject.Action(1, 2.0, "three");
                        testSubject.Action(1, 2.0, "three");
                        Expect(() => Expect(spy).ToHaveBeenCalledTimes(1)).ToThrow <JazExpectationException>();
                    });

                    Describe("with Not", () =>
                    {
                        It("should invert the check.", () =>
                        {
                            Expect(spy).Not.ToHaveBeenCalledTimes(1);

                            testSubject.Action(1, 2.0, "three");
                            Expect(() => Expect(spy).Not.ToHaveBeenCalledTimes(1)).ToThrow <JazExpectationException>();

                            testSubject.Action(1, 2.0, "three");
                            Expect(spy).Not.ToHaveBeenCalledTimes(1);
                        });
                    });
                });

                Describe(nameof(SpyExpect.ToHaveBeenCalledWith), () =>
                {
                    It("should fail if the spy was not called.", () =>
                    {
                        Expect(() => Expect(spy).ToHaveBeenCalledWith(1, 2.0, "three")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if the spy was not called with the expected parameters.", () =>
                    {
                        testSubject.Action(1, 2.0, "tree");
                        Expect(() => Expect(spy).ToHaveBeenCalledWith(1, 2.0, "three")).ToThrow <JazExpectationException>();
                    });

                    It("should pass if the spy was called with the expected parameters.", () =>
                    {
                        testSubject.Action(1, 2.0, "three");
                        Expect(spy).ToHaveBeenCalledWith(1, 2.0, "three");
                    });

                    Describe("with Not", () =>
                    {
                        It("should invert the check.", () =>
                        {
                            Expect(spy).Not.ToHaveBeenCalledWith(1, 2.0, "three");

                            testSubject.Action(1, 2.0, "tree");
                            Expect(spy).Not.ToHaveBeenCalledWith(1, 2.0, "three");

                            testSubject.Action(1, 2.0, "three");
                            Expect(() => Expect(spy).Not.ToHaveBeenCalledWith(1, 2.0, "three")).ToThrow <JazExpectationException>();
                        });
                    });
                });
            });
        }
Exemplo n.º 12
0
        public InstanceMethodSpec()
        {
            Describe("An Instance Action", () =>
            {
                var subject = new TestSubject();

                It("should call through by default.", () =>
                {
                    subject.Iterate(3);
                    Expect(subject.Value).ToBe(3);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Iterate));
                    subject.Iterate(3);
                    Expect(subject.Value).ToBeDefault();
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Iterate)).And.CallThrough();
                    subject.Iterate(3);
                    Expect(subject.Value).ToBe(3);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(subject, nameof(subject.Iterate)).And.Throw(exception);
                    var thrown = Expect(() => subject.Iterate(3)).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });
            });

            Describe("An Instance Function", () =>
            {
                var subject = new TestSubject();

                It("should call through by default.", () =>
                {
                    var result = subject.Add5(3);
                    Expect(result).ToBe(8);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5));
                    var result = subject.Add5(3);
                    Expect(result).ToBeDefault();
                });

                It("should return the configured value.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.ReturnValue(1);
                    var result = subject.Add5(3);
                    Expect(result).ToBe(1);
                });

                It("should return the configured values in sequence.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.ReturnValues(1, 2, 3, 4);
                    var result = subject.Add5(3);
                    Expect(result).ToBe(1);
                    result = subject.Add5(3);
                    Expect(result).ToBe(2);
                    result = subject.Add5(3);
                    Expect(result).ToBe(3);
                    result = subject.Add5(3);
                    Expect(result).ToBe(4);
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.CallThrough();
                    var result = subject.Add5(3);
                    Expect(result).ToBe(8);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.Throw(exception);
                    var thrown = Expect(() => subject.Add5(3)).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });
            });
        }
        public GenericInstanceMethodSpec()
        {
            Describe("A generic instance action", () =>
            {
                var subject = new TestSubject();

                It("should call through by default.", () =>
                {
                    subject.Iterate(3);
                    Expect(subject.Value).ToBe(3);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Iterate));
                    subject.Iterate(3);
                    Expect(subject.Value).ToBeDefault();
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Iterate)).And.CallThrough();
                    subject.Iterate(3);
                    Expect(subject.Value).ToBe(3);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(subject, nameof(subject.Iterate)).And.Throw(exception);
                    var thrown = Expect(() => subject.Iterate(3)).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });
            });

            Describe("A generic instance function", () =>
            {
                var subject = new TestSubject();

                It("should call through by default.", () =>
                {
                    var result = subject.Add5(3);
                    Expect(result).ToBe(8);
                });

                It("should do nothing if spied on.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5));
                    var result = subject.Add5(3);
                    Expect(result).ToBeDefault();
                });

                It("should return the configured value.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.ReturnValue(1);
                    var result = subject.Add5(3);
                    Expect(result).ToBe(1);
                });

                It("should return the configured values in sequence.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.ReturnValues(1, 2, 3, 4);
                    var result = subject.Add5(3);
                    Expect(result).ToBe(1);
                    result = subject.Add5(3);
                    Expect(result).ToBe(2);
                    result = subject.Add5(3);
                    Expect(result).ToBe(3);
                    result = subject.Add5(3);
                    Expect(result).ToBe(4);
                });

                It("should call through as configured.", () =>
                {
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.CallThrough();
                    var result = subject.Add5(3);
                    Expect(result).ToBe(8);
                });

                It("should throw an exception as configured.", () =>
                {
                    var exception = new TestException();
                    Jaz.SpyOn(subject, nameof(subject.Add5)).And.Throw(exception);
                    var thrown = Expect(() => subject.Add5(3)).ToThrow <TestException>();
                    Expect(thrown).ToBe(exception);
                });

                It("should support calling a generic method from a generic method.", () =>
                {
                    var testSubject = new TestSubject();
                    var result      = testSubject.CallFunc(() => 5);
                    Expect(result).ToBe(5);
                });

                It("should support calling a generic method from a generic class.", () =>
                {
                    var testSubject = new TestSubject <int>();
                    var result      = testSubject.CallFunc(() => 5);
                    Expect(result).ToBe(5);
                });

                It("should support calling a generic class from a generic method.", () =>
                {
                    var testSubject = new TestSubject();
                    var result      = testSubject.CallFunc2(() => 5);
                    Expect(result).ToBe(5);
                });

                It("should support calling a method on a nested class in a generic class.", () =>
                {
                    var testSubject = new TestSubject <int> .TestSubjectChild();
                    var result      = testSubject.CallFunc(() => 5);
                    Expect(result).ToBe(5);
                });
            });
        }
Exemplo n.º 14
0
        public AsyncAwaitSpec()
        {
            Describe("Async Await methods", () =>
            {
                Describe("from non-async method", () =>
                {
                    It("should call through by default.", () =>
                    {
                        TestSubject.InternalMethod(5).GetAwaiter().GetResult();
                    });
                });

                Describe("without a result", () =>
                {
                    It("should call through by default.", async() =>
                    {
                        await TestSubject.MethodOne(5);
                    });

                    // spies may be changed to return Task.CompletedTask in future
                    It("should do nothing if spied on.", () =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodOne));
                        var task = TestSubject.MethodOne(5);
                        Expect(task).ToBeDefault();
                    });

                    It("should call through as configured.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodOne)).And.CallThrough();
                        await TestSubject.MethodOne(5);
                    });

                    It("should throw an exception as configured.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodOne)).And.Throw <TestException>();
                        await ExpectAsync(() => TestSubject.MethodOne(5)).ToThrow <TestException>();
                    });

                    It("should throw an exception as configured.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodOne)).And.Throw <TestException>();
                        await ExpectAsync(async() => await TestSubject.MethodOne(5)).ToThrow <TestException>();
                    });
                });

                Describe("with a result", () =>
                {
                    It("should call through by default.", async() =>
                    {
                        var result = await TestSubject.MethodTwo(5);
                        Expect(result).ToBe("5");
                    });

                    // spies may be changed to return Task.CompletedTask in future
                    It("should do nothing if spied on.", () =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodTwo));
                        var task = TestSubject.MethodTwo(5);
                        Expect(task).ToBeDefault();
                    });

                    It("should call through as configured.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodTwo)).And.CallThrough();
                        var result = await TestSubject.MethodTwo(5);
                        Expect(result).ToBe("5");
                    });

                    It("should throw an exception as configured.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodOne)).And.Throw <TestException>();
                        await ExpectAsync(() => TestSubject.MethodOne(5)).ToThrow <TestException>();
                    });

                    It("should throw an exception as configured.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodOne)).And.Throw <TestException>();
                        await ExpectAsync(async() => await TestSubject.MethodOne(5)).ToThrow <TestException>();
                    });

                    It("should return the configured value.", async() =>
                    {
                        Jaz.SpyOn(typeof(TestSubject), nameof(TestSubject.MethodTwo)).And.ReturnValue(Task.FromResult("3"));
                        var result = await TestSubject.MethodTwo(5);
                        Expect(result).ToBe("3");
                    });
                });
            });
        }