Esempio 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);
                });
            });
        }
Esempio 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);
                    });
                });
            });
        }
Esempio 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);
                });
            });
        }
Esempio 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();
                    });
                });
            });
        }
Esempio 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();
                    });
                });
            });
        }
Esempio 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 });
                });
            });
        }
Esempio 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 }
                        }
                    });
                });
            });
        }
Esempio 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");
                    });
                });
            });
        }
Esempio n. 9
0
        public ValueExpectSpec()
        {
            Describe <ValueExpect <object> >(() =>
            {
                Describe(nameof(ValueExpect <object> .ToBe), () =>
                {
                    It("should fail if value types are not equal.", () =>
                    {
                        Expect(() => Expect(5).ToBe(4)).ToThrow <JazExpectationException>();
                    });

                    It("should fail if strings are not equal.", () =>
                    {
                        Expect(() => Expect("a").ToBe("A")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if classes are not same.", () =>
                    {
                        Expect(() => Expect(new object()).ToBe(new object())).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value types are equal.", () =>
                    {
                        Expect(5).ToBe(5);
                    });

                    It("should pass if strings are equal.", () =>
                    {
                        Expect("a").ToBe("a ".Trim());
                    });

                    It("should pass if classes are same.", () =>
                    {
                        var instance = new object();
                        Expect(instance).ToBe(instance);
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass if value types are not equal.", () =>
                        {
                            Expect(5).Not.ToBe(4);
                        });

                        It("should pass if strings are not equal.", () =>
                        {
                            Expect("a").Not.ToBe("A");
                        });

                        It("should pass if classes are not same.", () =>
                        {
                            Expect(new object()).Not.ToBe(new object());
                        });

                        It("should fail if value types are equal.", () =>
                        {
                            Expect(() => Expect(5).ToBe(5)).Not.ToThrow <JazExpectationException>();
                        });

                        It("should fail if strings are equal.", () =>
                        {
                            Expect(() => Expect("a").ToBe("a ".Trim())).Not.ToThrow <JazExpectationException>();
                        });

                        It("should fail if classes are same.", () =>
                        {
                            var instance = new object();
                            Expect(() => Expect(instance).ToBe(instance)).Not.ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeBetween), () =>
                {
                    It("should fail if value is too low.", () =>
                    {
                        Expect(() => Expect(5).ToBeBetween(6, 9)).ToThrow <JazExpectationException>();
                    });

                    It("should fail if value is too high.", () =>
                    {
                        Expect(() => Expect(10).ToBeBetween(6, 9)).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value is equal to min.", () =>
                    {
                        Expect(6).ToBeBetween(6, 9);
                    });

                    It("should pass if value is equal to max.", () =>
                    {
                        Expect(9).ToBeBetween(6, 9);
                    });

                    It("should pass if value is inside the range.", () =>
                    {
                        Expect(7).ToBeBetween(6, 9);
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass if value is too low.", () =>
                        {
                            Expect(5).Not.ToBeBetween(6, 9);
                        });

                        It("should pass if value is too high.", () =>
                        {
                            Expect(10).Not.ToBeBetween(6, 9);
                        });

                        It("should fail if value is equal to min.", () =>
                        {
                            Expect(() => Expect(6).Not.ToBeBetween(6, 9)).ToThrow <JazExpectationException>();
                        });

                        It("should fail if value is equal to max.", () =>
                        {
                            Expect(() => Expect(9).Not.ToBeBetween(6, 9)).ToThrow <JazExpectationException>();
                        });

                        It("should fail if value is inside the range.", () =>
                        {
                            Expect(() => Expect(7).Not.ToBeBetween(6, 9)).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeDefault), () =>
                {
                    It("should fail on an instance.", () =>
                    {
                        Expect(() => Expect(new object()).ToBeDefault()).ToThrow <JazExpectationException>();
                    });

                    It("should fail on non-default value.", () =>
                    {
                        Expect(() => Expect(1).ToBeDefault()).ToThrow <JazExpectationException>();
                    });

                    It("should pass on null.", () =>
                    {
                        Expect((object)null).ToBeDefault();
                    });

                    It("should pass on zero.", () =>
                    {
                        Expect(0).ToBeDefault();
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass on an instance.", () =>
                        {
                            Expect(new object()).Not.ToBeDefault();
                        });

                        It("should pass on non-default value.", () =>
                        {
                            Expect(1).Not.ToBeDefault();
                        });

                        It("should fail on null.", () =>
                        {
                            Expect(() => Expect((object)null).Not.ToBeDefault()).ToThrow <JazExpectationException>();
                        });

                        It("should pass on zero.", () =>
                        {
                            Expect(() => Expect(0).Not.ToBeDefault()).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeEmpty), () =>
                {
                    It("should fail on an array with items.", () =>
                    {
                        Expect(() => Expect(new[] { 1 }).ToBeEmpty()).ToThrow <JazExpectationException>();
                    });

                    It("should fail on a non-empty string.", () =>
                    {
                        Expect(() => Expect("a").ToBeEmpty()).ToThrow <JazExpectationException>();
                    });

                    It("should pass on an empty array.", () =>
                    {
                        Expect(new int[0]).ToBeEmpty();
                    });

                    It("should pass on an empty string.", () =>
                    {
                        Expect("").ToBeEmpty();
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass on an array with items.", () =>
                        {
                            Expect(new[] { 1 }).Not.ToBeEmpty();
                        });

                        It("should pass on a non-empty string.", () =>
                        {
                            Expect("a").Not.ToBeEmpty();
                        });

                        It("should fail on an empty array.", () =>
                        {
                            Expect(() => Expect(new int[0]).Not.ToBeEmpty()).ToThrow <JazExpectationException>();
                        });

                        It("should fail on an empty string.", () =>
                        {
                            Expect(() => Expect("").Not.ToBeEmpty()).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeFalse), () =>
                {
                    It("should fail on non-boolean values.", () =>
                    {
                        Expect(() => Expect(5).ToBeFalse()).ToThrow <JazExpectationException>();
                    });

                    It("should fail on null.", () =>
                    {
                        Expect(() => Expect((object)null).ToBeFalse()).ToThrow <JazExpectationException>();
                    });

                    It("should fail on true.", () =>
                    {
                        Expect(() => Expect(true).ToBeFalse()).ToThrow <JazExpectationException>();
                    });

                    It("should pass on false.", () =>
                    {
                        Expect(false).ToBeFalse();
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass on non-boolean values.", () =>
                        {
                            Expect(5).Not.ToBeFalse();
                        });

                        It("should pass on null.", () =>
                        {
                            Expect((object)null).Not.ToBeFalse();
                        });

                        It("should pass on true.", () =>
                        {
                            Expect(true).Not.ToBeFalse();
                        });

                        It("should fail on false.", () =>
                        {
                            Expect(() => Expect(false).Not.ToBeFalse()).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeTrue), () =>
                {
                    It("should fail on non-boolean values.", () =>
                    {
                        Expect(() => Expect(5).ToBeTrue()).ToThrow <JazExpectationException>();
                    });

                    It("should fail on null.", () =>
                    {
                        Expect(() => Expect((object)null).ToBeTrue()).ToThrow <JazExpectationException>();
                    });

                    It("should fail on false.", () =>
                    {
                        Expect(() => Expect(false).ToBeTrue()).ToThrow <JazExpectationException>();
                    });

                    It("should pass on true.", () =>
                    {
                        Expect(true).ToBeTrue();
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass on non-boolean values.", () =>
                        {
                            Expect(5).Not.ToBeTrue();
                        });

                        It("should pass on null.", () =>
                        {
                            Expect((object)null).Not.ToBeTrue();
                        });

                        It("should pass on false.", () =>
                        {
                            Expect(false).Not.ToBeTrue();
                        });

                        It("should fail on true.", () =>
                        {
                            Expect(() => Expect(true).Not.ToBeTrue()).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToEqual), () =>
                {
                    It("should fail on mismatching value types.", () =>
                    {
                        Expect(() => Expect(1).ToEqual(2)).ToThrow <JazExpectationException>();
                    });

                    It("should fail on different list lengths", () =>
                    {
                        Expect(() => Expect(new[] { 3 }).ToEqual(new int[0])).ToThrow <JazExpectationException>();
                    });

                    It("should fail on different list items.", () =>
                    {
                        Expect(() => Expect(new[] { 3 }).ToEqual(new[] { 1 })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if expected has extra properties.", () =>
                    {
                        Expect(() => Expect(new { }).ToEqual(new { v = 0 })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if expected is missing properties.", () =>
                    {
                        Expect(() => Expect(new { v = 0 }).ToEqual(new { })).ToThrow <JazExpectationException>();
                    });

                    It("should fail when comparing to Jaz.Any<T> of wrong type.", () =>
                    {
                        Expect(() => Expect(new { v = 0 }).ToEqual(new { v = Jaz.Any <double>() })).ToThrow <JazExpectationException>();
                    });

                    It("should fail when comparing to Jaz.InstanceOf<T> with inheriting value.", () =>
                    {
                        Expect(() => Expect(new { v = "" }).ToEqual(new { v = Jaz.InstanceOf <object>() })).ToThrow <JazExpectationException>();
                    });

                    It("should fail when comparing null to Jaz.InstanceOf<T> with nullable type.", () =>
                    {
                        Expect(() => Expect(new { v = (object)null }).ToEqual(new { v = Jaz.InstanceOf <object>() })).ToThrow <JazExpectationException>();
                    });

                    It("should pass on equivalent value types.", () =>
                    {
                        Expect(3).ToEqual(3);
                    });

                    It("should pass on equivalent reference types.", () =>
                    {
                        Expect("yes").ToEqual("yes ".Trim());
                    });

                    It("should pass on equivalent list lengths.", () =>
                    {
                        Expect(new int[0]).ToEqual(new object[0]);
                    });

                    It("should pass on equivalent list items.", () =>
                    {
                        Expect(new[] { "a", "b", "c" }).ToEqual(new[] { " a ".Trim(), " b ".Trim(), " c ".Trim() });
                    });

                    It("should pass on objects with the same properies with equivalent values.", () =>
                    {
                        Expect(new { v = "a" }).ToEqual(new { v = " a ".Trim() });
                    });

                    It("should pass when comparing to Jaz.Any<T>.", () =>
                    {
                        Expect(new { v = 1 }).ToEqual(new { v = Jaz.Any <int>() });
                    });

                    It("should pass when comparing to Jaz.Any.", () =>
                    {
                        Expect(new { v = 1 }).ToEqual(new { v = Jaz.Any() });
                    });

                    It("should pass when comparing to Jaz.AnyOrNull<T> when value is null and T is nullable.", () =>
                    {
                        Expect(new { v = (object)null }).ToEqual(new { v = Jaz.AnyOrNull <string>() });
                    });

                    It("should pass when comparing to Jaz.AnyOrNull when value is null.", () =>
                    {
                        Expect(new { v = (int?)null }).ToEqual(new { v = Jaz.AnyOrNull() });
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass on mismatching value types.", () =>
                        {
                            Expect(1).Not.ToEqual(2);
                        });

                        It("should pass on different list lengths", () =>
                        {
                            Expect(new[] { 3 }).Not.ToEqual(new int[0]);
                        });

                        It("should pass on different list items.", () =>
                        {
                            Expect(new[] { 3 }).Not.ToEqual(new[] { 1 });
                        });

                        It("should pass if expected has extra properties.", () =>
                        {
                            Expect(new { }).Not.ToEqual(new { v = 0 });
                        });

                        It("should pass if expected is missing properties.", () =>
                        {
                            Expect(new { v = 0 }).Not.ToEqual(new { });
                        });

                        It("should pass when comparing to Jaz.Any<T> of wrong type.", () =>
                        {
                            Expect(new { v = 0 }).Not.ToEqual(new { v = Jaz.Any <double>() });
                        });

                        It("should pass when comparing to Jaz.InstanceOf<T> with inheriting value.", () =>
                        {
                            Expect(new { v = "" }).Not.ToEqual(new { v = Jaz.InstanceOf <object>() });
                        });

                        It("should pass when comparing null to Jaz.InstanceOf<T> with nullable type.", () =>
                        {
                            Expect(new { v = (object)null }).Not.ToEqual(new { v = Jaz.InstanceOf <object>() });
                        });

                        It("should fail on equivalent value types.", () =>
                        {
                            Expect(() => Expect(3).Not.ToEqual(3)).ToThrow <JazExpectationException>();
                        });

                        It("should fail on equivalent reference types.", () =>
                        {
                            Expect(() => Expect("yes").Not.ToEqual("yes ".Trim())).ToThrow <JazExpectationException>();
                        });

                        It("should fail on equivalent list lengths.", () =>
                        {
                            Expect(() => Expect(new int[0]).Not.ToEqual(new object[0])).ToThrow <JazExpectationException>();
                        });

                        It("should fail on equivalent list items.", () =>
                        {
                            Expect(() => Expect(new[] { "a", "b", "c" }).Not.ToEqual(new[] { " a ".Trim(), " b ".Trim(), " c ".Trim() })).ToThrow <JazExpectationException>();
                        });

                        It("should fail on objects with the same properies with equivalent values.", () =>
                        {
                            Expect(() => Expect(new { v = "a" }).Not.ToEqual(new { v = " a ".Trim() })).ToThrow <JazExpectationException>();
                        });

                        It("should fail when comparing to Jaz.Any<T>.", () =>
                        {
                            Expect(() => Expect(new { v = 1 }).Not.ToEqual(new { v = Jaz.Any <int>() })).ToThrow <JazExpectationException>();
                        });

                        It("should fail when comparing to Jaz.Any.", () =>
                        {
                            Expect(() => Expect(new { v = 1 }).Not.ToEqual(new { v = Jaz.Any() })).ToThrow <JazExpectationException>();
                        });

                        It("should fail when comparing to Jaz.AnyOrNull<T> when value is null and T is nullable.", () =>
                        {
                            Expect(() => Expect(new { v = (object)null }).Not.ToEqual(new { v = Jaz.AnyOrNull <string>() })).ToThrow <JazExpectationException>();
                        });

                        It("should fail when comparing to Jaz.AnyOrNull when value is null.", () =>
                        {
                            Expect(() => Expect(new { v = (int?)null }).Not.ToEqual(new { v = Jaz.AnyOrNull() })).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeGreaterThan), () =>
                {
                    It("should fail if value is less than the given value.", () =>
                    {
                        Expect(() => Expect(3).ToBeGreaterThan(5)).ToThrow <JazExpectationException>();
                    });

                    It("should fail if value is equal to the given value.", () =>
                    {
                        Expect(() => Expect(5).ToBeGreaterThan(5)).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value is greater than the given value.", () =>
                    {
                        Expect(7).ToBeGreaterThan(5);
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass if value is less than the given value.", () =>
                        {
                            Expect(3).Not.ToBeGreaterThan(5);
                        });

                        It("should pass if value is equal to the given value.", () =>
                        {
                            Expect(5).Not.ToBeGreaterThan(5);
                        });

                        It("should fail if value is greater than the given value.", () =>
                        {
                            Expect(() => Expect(7).Not.ToBeGreaterThan(5)).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeLessThan), () =>
                {
                    It("should fail if value is less than the given value.", () =>
                    {
                        Expect(() => Expect(7).ToBeLessThan(5)).ToThrow <JazExpectationException>();
                    });

                    It("should fail if value is equal to the given value.", () =>
                    {
                        Expect(() => Expect(5).ToBeLessThan(5)).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value is greater than the given value.", () =>
                    {
                        Expect(3).ToBeLessThan(5);
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass if value is greater than the given value.", () =>
                        {
                            Expect(7).Not.ToBeLessThan(5);
                        });

                        It("should pass if value is equal to the given value.", () =>
                        {
                            Expect(5).Not.ToBeLessThan(5);
                        });

                        It("should fail if value is less than the given value.", () =>
                        {
                            Expect(() => Expect(3).Not.ToBeLessThan(5)).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeGreaterThanOrEqualTo), () =>
                {
                    It("should fail if value is less than the given value.", () =>
                    {
                        Expect(() => Expect(3).ToBeGreaterThanOrEqualTo(5)).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value is equal to the given value.", () =>
                    {
                        Expect(5).ToBeGreaterThanOrEqualTo(5);
                    });

                    It("should pass if value is greater than the given value.", () =>
                    {
                        Expect(7).ToBeGreaterThanOrEqualTo(5);
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass if value is less than the given value.", () =>
                        {
                            Expect(3).Not.ToBeGreaterThanOrEqualTo(5);
                        });

                        It("should fail if value is equal to the given value.", () =>
                        {
                            Expect(() => Expect(5).Not.ToBeGreaterThanOrEqualTo(5)).ToThrow <JazExpectationException>();
                        });

                        It("should fail if value is greater than the given value.", () =>
                        {
                            Expect(() => Expect(7).Not.ToBeGreaterThanOrEqualTo(5)).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToBeLessThanOrEqualTo), () =>
                {
                    It("should fail if value is less than the given value.", () =>
                    {
                        Expect(() => Expect(7).ToBeLessThanOrEqualTo(5)).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value is equal to the given value.", () =>
                    {
                        Expect(5).ToBeLessThanOrEqualTo(5);
                    });

                    It("should pass if value is greater than the given value.", () =>
                    {
                        Expect(3).ToBeLessThanOrEqualTo(5);
                    });

                    Describe("with Not", () =>
                    {
                        It("should pass if value is greater than the given value.", () =>
                        {
                            Expect(7).Not.ToBeLessThanOrEqualTo(5);
                        });

                        It("should fail if value is equal to the given value.", () =>
                        {
                            Expect(() => Expect(5).Not.ToBeLessThanOrEqualTo(5)).ToThrow <JazExpectationException>();
                        });

                        It("should fail if value is less than the given value.", () =>
                        {
                            Expect(() => Expect(3).Not.ToBeLessThanOrEqualTo(5)).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <string> .ToMatch), () =>
                {
                    It("should fail if the value is not a string.", () =>
                    {
                        Expect(() => Expect(12).ToMatch("[0-9]+")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if the value is not a string.", () =>
                    {
                        Expect(() => Expect(12).ToMatch(new Regex("[0-9]+"))).ToThrow <JazExpectationException>();
                    });

                    It("should fail if the value does not match the regex.", () =>
                    {
                        Expect(() => Expect("abc").ToMatch("[ab]+d")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if the value does not match the regex.", () =>
                    {
                        Expect(() => Expect("abc").ToMatch(new Regex("[ab]+d"))).ToThrow <JazExpectationException>();
                    });

                    It("should pass if the value does matches the regex.", () =>
                    {
                        Expect("abc").ToMatch("[ab]+c");
                    });

                    It("should pass if the value does matches the regex.", () =>
                    {
                        Expect("abc").ToMatch(new Regex("[ab]+c"));
                    });

                    Describe("with Not", () =>
                    {
                        It("should still fail if the value is not a string.", () =>
                        {
                            Expect(() => Expect(12).Not.ToMatch("[0-9]+")).ToThrow <JazExpectationException>();
                        });

                        It("should still fail if the value is not a string.", () =>
                        {
                            Expect(() => Expect(12).Not.ToMatch(new Regex("[0-9]+"))).ToThrow <JazExpectationException>();
                        });

                        It("should pass if the value does not match the regex.", () =>
                        {
                            Expect("abc").Not.ToMatch("[ab]+d");
                        });

                        It("should pass if the value does not match the regex.", () =>
                        {
                            Expect("abc").Not.ToMatch(new Regex("[ab]+d"));
                        });

                        It("should fail if the value does matches the regex.", () =>
                        {
                            Expect(() => Expect("abc").Not.ToMatch("[ab]+c")).ToThrow <JazExpectationException>();
                        });

                        It("should fail if the value does matches the regex.", () =>
                        {
                            Expect(() => Expect("abc").Not.ToMatch(new Regex("[ab]+c"))).ToThrow <JazExpectationException>();
                        });
                    });
                });

                Describe(nameof(ValueExpect <object> .ToContain), () =>
                {
                    It("should fail if value is not string and checking against a string value.", () =>
                    {
                        Expect(() => Expect(12).ToContain("12")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if value does not contain the string.", () =>
                    {
                        Expect(() => Expect("1234").ToContain("14")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if value doesn't match given string casing.", () =>
                    {
                        Expect(() => Expect("aBcD").ToContain("bc")).ToThrow <JazExpectationException>();
                    });

                    It("should fail if list is missing the given value.", () =>
                    {
                        Expect(() => Expect(new[] { "a", "b", "c" }).ToContain(new[] { "a", "d" })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if object is missing properties.", () =>
                    {
                        Expect(() => Expect(new { p1 = "x", p2 = "y" }).ToContain(new { p1 = "x", p3 = "z" })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if list in object is missing values.", () =>
                    {
                        Expect(() => Expect(new { p1 = new[] { "a", "b", "c" } }).ToContain(new { p1 = new[] { "a", "d" } })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if object in list is missing properties.", () =>
                    {
                        Expect(() => Expect(new[] { new { p1 = "x", p2 = "y" } }).ToContain(new[] { new { p1 = "x", p3 = "z" } })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if duplicate items are expected but missing.", () =>
                    {
                        Expect(() => Expect(new[] { "a", "b" }).ToContain(new[] { "b", "b" })).ToThrow <JazExpectationException>();
                    });

                    It("should fail if testing similar items in list in unexpected order.", () =>
                    {
                        Expect(() => Expect(new object[] { new { p1 = "x", p2 = "y", p3 = "z" }, new { p1 = "x", p2 = "y" } })
                               .ToContain(new object[] { new { p1 = "x" }, new { p1 = "x", p3 = "z" } })).ToThrow <JazExpectationException>();
                    });

                    It("should pass if value contains string.", () =>
                    {
                        Expect("1234").ToContain("23");
                    });

                    It("should pass if value casing is different but ignore casing is true.", () =>
                    {
                        Expect("aBcD").ToContain("bc", true);
                    });

                    It("should pass if list contains given items.", () =>
                    {
                        Expect(new[] { "a", "b", "c" }).ToContain(new[] { "b", "c" });
                    });

                    It("should pass if object contains given properties.", () =>
                    {
                        Expect(new { p1 = "x", p2 = "y", p3 = "z" }).ToContain(new { p2 = "y", p3 = "z" });
                    });

                    It("should pass if list in object contains expect items.", () =>
                    {
                        Expect(new { list = new[] { "a", "b", "c" } }).ToContain(new { list = new[] { "b", "c" } });
                    });

                    It("should pass if object in list contains expected properties.", () =>
                    {
                        Expect(new[] { new { p1 = "x", p2 = "y", p3 = "z" } }).ToContain(new[] { new { p2 = "y", p3 = "z" } });
                    });

                    It("should pass if testing similar items in list in correct order.", () =>
                    {
                        Expect(new object[] { new { p1 = "x", p2 = "y" }, new { p1 = "x", p2 = "y", p3 = "z" } })
                        .ToContain(new object[] { new { p1 = "x" }, new { p1 = "x", p3 = "z" } });
                    });
                });
            });
        }
Esempio n. 10
0
        public PropertySpec()
        {
            Describe("Instance Property", () =>
            {
                TestSubject testSubject = null;

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

                Describe("with get and set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(testSubject.GetSetProp).ToBe("123");
                        testSubject.GetSetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp));
                        testSubject.GetSetProp = "test.";

                        Expect(testSubject.GetSetProp).ToBeDefault();
                        Expect(testSubject.Value).ToBe("123");
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp)).Getter.And.ReturnValue("test.");
                        Expect(testSubject.GetSetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp));
                        propertySpy.Getter.And.CallThrough();
                        propertySpy.Setter.And.CallThrough();

                        Expect(testSubject.GetSetProp).ToBe("123");
                        testSubject.GetSetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp));
                        propertySpy.Getter.And.Throw <TestException>();
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => { var x = testSubject.GetSetProp; }).ToThrow <TestException>();
                        Expect(() => testSubject.GetSetProp = "test.").ToThrow <TestException>();
                    });
                });

                Describe("with only get", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(testSubject.GetProp).ToBe("123");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp));
                        Expect((object)propertySpy.Setter).ToBeDefault();
                        Expect(testSubject.GetProp).ToBeDefault();
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp)).Getter.And.ReturnValue("test.");
                        Expect(testSubject.GetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp));
                        propertySpy.Getter.And.CallThrough();

                        Expect(testSubject.GetProp).ToBe("123");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp));
                        propertySpy.Getter.And.Throw <TestException>();

                        Expect(() => { var x = testSubject.GetProp; }).ToThrow <TestException>();
                    });
                });

                Describe("with only set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        testSubject.SetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy     = Jaz.SpyOnProperty(testSubject, nameof(testSubject.SetProp));
                        testSubject.SetProp = "test.";

                        Expect((object)propertySpy.Getter).ToBeDefault();
                        Expect(testSubject.Value).ToBe("123");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.SetProp));
                        propertySpy.Setter.And.CallThrough();

                        testSubject.SetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.SetProp));
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => testSubject.SetProp = "test.").ToThrow <TestException>();
                    });
                });
            });

            Describe("Static Property", () =>
            {
                BeforeEach(() =>
                {
                    TestSubject.StaticValue = "abc";
                });

                Describe("with get and set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(TestSubject.StaticGetSetProp).ToBe("abc");
                        TestSubject.StaticGetSetProp = "test.";
                        Expect(TestSubject.StaticValue).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetSetProp));
                        TestSubject.StaticGetSetProp = "test.";

                        Expect(TestSubject.StaticGetSetProp).ToBeDefault();
                        Expect(TestSubject.StaticValue).ToBe("abc");
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetSetProp)).Getter.And.ReturnValue("test.");
                        Expect(TestSubject.StaticGetSetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetSetProp));
                        propertySpy.Getter.And.CallThrough();
                        propertySpy.Setter.And.CallThrough();

                        Expect(TestSubject.StaticGetSetProp).ToBe("abc");
                        TestSubject.StaticGetSetProp = "test.";
                        Expect(TestSubject.StaticValue).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetSetProp));
                        propertySpy.Getter.And.Throw <TestException>();
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => { var x = TestSubject.StaticGetSetProp; }).ToThrow <TestException>();
                        Expect(() => TestSubject.StaticGetSetProp = "test.").ToThrow <TestException>();
                    });
                });

                Describe("with only get", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(TestSubject.StaticGetProp).ToBe("abc");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetProp));
                        Expect((object)propertySpy.Setter).ToBeDefault();
                        Expect(TestSubject.StaticGetProp).ToBeDefault();
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetProp)).Getter.And.ReturnValue("test.");
                        Expect(TestSubject.StaticGetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetProp));
                        propertySpy.Getter.And.CallThrough();

                        Expect(TestSubject.StaticGetProp).ToBe("abc");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticGetProp));
                        propertySpy.Getter.And.Throw <TestException>();

                        Expect(() => { var x = TestSubject.StaticGetProp; }).ToThrow <TestException>();
                    });
                });

                Describe("with only set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        TestSubject.StaticSetProp = "test.";
                        Expect(TestSubject.StaticValue).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy           = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticSetProp));
                        TestSubject.StaticSetProp = "test.";

                        Expect((object)propertySpy.Getter).ToBeDefault();
                        Expect(TestSubject.StaticValue).ToBe("abc");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticSetProp));
                        propertySpy.Setter.And.CallThrough();

                        TestSubject.StaticSetProp = "test.";
                        Expect(TestSubject.StaticValue).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject), nameof(TestSubject.StaticSetProp));
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => TestSubject.StaticSetProp = "test.").ToThrow <TestException>();
                    });
                });
            });

            Describe("Generic Instance Property", () =>
            {
                TestSubject <string> testSubject = null;

                BeforeEach(() =>
                {
                    testSubject       = new TestSubject <string>();
                    testSubject.Value = "123";
                });

                Describe("with get and set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(testSubject.GetSetProp).ToBe("123");
                        testSubject.GetSetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp));
                        testSubject.GetSetProp = "test.";

                        Expect(testSubject.GetSetProp).ToBeDefault();
                        Expect(testSubject.Value).ToBe("123");
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp)).Getter.And.ReturnValue("test.");
                        Expect(testSubject.GetSetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp));
                        propertySpy.Getter.And.CallThrough();
                        propertySpy.Setter.And.CallThrough();

                        Expect(testSubject.GetSetProp).ToBe("123");
                        testSubject.GetSetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetSetProp));
                        propertySpy.Getter.And.Throw <TestException>();
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => { var x = testSubject.GetSetProp; }).ToThrow <TestException>();
                        Expect(() => testSubject.GetSetProp = "test.").ToThrow <TestException>();
                    });
                });

                Describe("with only get", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(testSubject.GetProp).ToBe("123");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp));
                        Expect((object)propertySpy.Setter).ToBeDefault();
                        Expect(testSubject.GetProp).ToBeDefault();
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp)).Getter.And.ReturnValue("test.");
                        Expect(testSubject.GetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp));
                        propertySpy.Getter.And.CallThrough();

                        Expect(testSubject.GetProp).ToBe("123");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.GetProp));
                        propertySpy.Getter.And.Throw <TestException>();

                        Expect(() => { var x = testSubject.GetProp; }).ToThrow <TestException>();
                    });
                });

                Describe("with only set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        testSubject.SetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy     = Jaz.SpyOnProperty(testSubject, nameof(testSubject.SetProp));
                        testSubject.SetProp = "test.";

                        Expect((object)propertySpy.Getter).ToBeDefault();
                        Expect(testSubject.Value).ToBe("123");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.SetProp));
                        propertySpy.Setter.And.CallThrough();

                        testSubject.SetProp = "test.";
                        Expect(testSubject.Value).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(testSubject, nameof(testSubject.SetProp));
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => testSubject.SetProp = "test.").ToThrow <TestException>();
                    });
                });
            });

            Describe("Generic Static Property", () =>
            {
                BeforeEach(() =>
                {
                    TestSubject <string> .StaticValue = "abc";
                });

                Describe("with get and set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(TestSubject <string> .StaticGetSetProp).ToBe("abc");
                        TestSubject <string> .StaticGetSetProp = "test.";
                        Expect(TestSubject <string> .StaticValue).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetSetProp));
                        TestSubject.StaticGetSetProp = "test.";

                        Expect(TestSubject <string> .StaticGetSetProp).ToBeDefault();
                        Expect(TestSubject <string> .StaticValue).ToBe("abc");
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetSetProp)).Getter.And.ReturnValue("test.");
                        Expect(TestSubject <string> .StaticGetSetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetSetProp));
                        propertySpy.Getter.And.CallThrough();
                        propertySpy.Setter.And.CallThrough();

                        Expect(TestSubject <string> .StaticGetSetProp).ToBe("abc");
                        TestSubject <string> .StaticGetSetProp = "test.";
                        Expect(TestSubject <string> .StaticValue).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetSetProp));
                        propertySpy.Getter.And.Throw <TestException>();
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => { var x = TestSubject <string> .StaticGetSetProp; }).ToThrow <TestException>();
                        Expect(() => TestSubject <string> .StaticGetSetProp = "test.").ToThrow <TestException>();
                    });
                });

                Describe("with only get", () =>
                {
                    It("should call through by default.", () =>
                    {
                        Expect(TestSubject <string> .StaticGetProp).ToBe("abc");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetProp));
                        Expect((object)propertySpy.Setter).ToBeDefault();
                        Expect(TestSubject <string> .StaticGetProp).ToBeDefault();
                    });

                    It("should return the configured value.", () =>
                    {
                        var spy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetProp)).Getter.And.ReturnValue("test.");
                        Expect(TestSubject <string> .StaticGetProp).ToBe("test.");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetProp));
                        propertySpy.Getter.And.CallThrough();

                        Expect(TestSubject <string> .StaticGetProp).ToBe("abc");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticGetProp));
                        propertySpy.Getter.And.Throw <TestException>();

                        Expect(() => { var x = TestSubject <string> .StaticGetProp; }).ToThrow <TestException>();
                    });
                });

                Describe("with only set", () =>
                {
                    It("should call through by default.", () =>
                    {
                        TestSubject <string> .StaticSetProp = "test.";
                        Expect(TestSubject <string> .StaticValue).ToBe("test.");
                    });

                    It("should do nothing if spied on.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticSetProp));
                        TestSubject <string> .StaticSetProp = "test.";

                        Expect((object)propertySpy.Getter).ToBeDefault();
                        Expect(TestSubject <string> .StaticValue).ToBe("abc");
                    });

                    It("should call through as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticSetProp));
                        propertySpy.Setter.And.CallThrough();

                        TestSubject <string> .StaticSetProp = "test.";
                        Expect(TestSubject <string> .StaticValue).ToBe("test.");
                    });

                    It("should throw an exception as configured.", () =>
                    {
                        var propertySpy = Jaz.SpyOnProperty(typeof(TestSubject <string>), nameof(TestSubject <string> .StaticSetProp));
                        propertySpy.Setter.And.Throw <TestException>();

                        Expect(() => TestSubject <string> .StaticSetProp = "test.").ToThrow <TestException>();
                    });
                });
            });
        }
Esempio n. 11
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);
                });
            });
Esempio n. 12
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);
                });
            });
        }
Esempio n. 13
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>();
                        });
                    });
                });
            });
        }
        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);
                });
            });
        }
Esempio n. 16
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");
                    });
                });
            });
        }