Пример #1
0
        public LogSpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with a single line log message"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var result = "";

                    before = () =>
                    {
                        specification.it["logs a message"] = () => 
                        {
                            result = specification.log("Hello Mum");
                        };
                        specification.Execute();
                    };

                    it["logs the message indented within the step"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "  logs a message" + Environment.NewLine +
                            "    Hello Mum"
                        );
                    };

                    it["returns the message"] = () =>
                    {
                        result.ShouldEqual("Hello Mum");
                    };
                };

                context["with a multi-line log message"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var result = "";

                    before = () =>
                    {
                        specification.it["logs a message"] = () =>
                        {
                            result = specification.log("Hello\r\nMum");
                        };
                        specification.Execute();
                    };

                    it["applies the correct indentation to each line of the message"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "  logs a message" + Environment.NewLine +
                            "    Hello" + Environment.NewLine +
                            "    Mum"
                        );
                    };
                };
            };
        }
Пример #2
0
        public ActivitySpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with multiple activities"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executed = 0;

                    before = () =>
                    {
                        specification.Given["I do one activity"] = () => executed++;
                        specification.When["I do another activity"] = () => executed++;
                        specification.Then["I'm very tired"] = () => { };
                        specification.Execute();
                    };

                    it["runs the code in each activity"] = () =>
                    {
                        executed.ShouldEqual(2);
                    };

                    it["writes each activity to the logger"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("Given I do one activity");
                        specification.Logger.ToString().ShouldContain("When I do another activity");
                    };
                };

                context["with a failing activity"] = () => 
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executed = 0;

                    before = () =>
                    {
                        specification.Given["An explosion"] = () => { throw new ArithmeticException(); };
                        specification.When["I do another activity"] = () => executed++;
                        specification.Then["I'm very tired"] = () => executed++;
                        Catch(() => specification.Execute());
                    };

                    it["writes the activity to the logger before bombing out"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("An explosion");
                    };

                    it["skips the subsequent activities and assertions"] = () =>
                    {
                        executed.ShouldEqual(0);
                    };
                };
            };
        }
Пример #3
0
        public ContextSpecification()
        {
            describe["Executing a specificaton"] = () =>
            {
                context["with a single context"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };

                    before = () =>
                    {
                        specification.describe["something"] = () =>
                        {
                            specification.it["does something"] = () => { };
                            specification.it["does something else"] = () => { };
                        };
                        specification.Execute();
                    };

                    it["logs each assertion indented within the context"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "  something" + Environment.NewLine +
                            "    does something" + Environment.NewLine +
                            "    does something else"
                        );
                    };
                };

                context["with nested contexts"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };

                    before = () =>
                    {
                        specification.describe["something"] = () =>
                        {
                            specification.context["in one context"] = () =>
                            {
                                specification.it["does one thing"] = () => { };
                            };

                            specification.context["in another context"] = () =>
                            {
                                specification.it["does another thing"] = () => { };
                            };
                        };
                        specification.Execute();
                    };

                    it["logs each context indented within their parent"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "  something" + Environment.NewLine +
                            "    in one context" + Environment.NewLine +
                            "      does one thing" + Environment.NewLine +
                            "    in another context" + Environment.NewLine +
                            "      does another thing"
                        );
                    };
                };
            };

            describe["Executing a single test"] = () =>
            {
                context["with multiple contexts"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var setupCount    = 0;
                    var activityCount = 0;

                    before = () =>
                    {
                        specification.before_each = () => setupCount++;
                        specification.before = () => activityCount++;

                        specification.context["context A"] = () =>
                        {
                            specification.before_each = () => setupCount++;
                            specification.before = () => activityCount++;
                        };

                        specification.context["context B"] = () =>
                        {
                            specification.it["assertion B1"] = () => { };
                        };

                        specification.Execute();
                    };

                    it["doesn't execute setup steps in other contexts"] = () =>
                    {
                        setupCount.ShouldEqual(1);
                    };

                    it["doesn't execute activities in other contexts"] = () =>
                    {
                        activityCount.ShouldEqual(1);
                    };
                };
            };
        }
Пример #4
0
        public AssertionSpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with a single assertion"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executed = false;

                    before = () =>
                    {
                        specification.it["does something"] = () => { executed = true; };
                        specification.Execute();
                    };

                    it["runs the code in the assertion"] = () =>
                    {
                        executed.ShouldBeTrue();
                    };

                    it["writes the assertion's title to the logger"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("does something");
                    };
                };

                context["with multiple assertions"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executed = 0;

                    before = () =>
                    {
                        specification.it["does something"] = () => executed++;
                        specification.it["does something else"] = () => executed++;
                        specification.Execute();
                    };

                    it["runs the code in each assertion"] = () =>
                    {
                        executed.ShouldEqual(2);
                    };

                    it["writes each assertion to the logger"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("does something");
                        specification.Logger.ToString().ShouldContain("does something else");
                    };
                };

                context["with a failing assertion"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    AggregateException exception = null;

                    before = () =>
                    {
                        specification.it["explodes"] = () => { throw new ArithmeticException(); };
                        exception = Catch<AggregateException>(() => specification.Execute());
                    };

                    it["writes the assertion to the logger before bombing out"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("explodes");
                    };

                    it["throws the exception"] = () =>
                    {
                        exception.InnerExceptions[0].ShouldBeOfExactType<ArithmeticException>();
                    };
                };
            };
        }
Пример #5
0
        public CommentSpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with a single line comment"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    
                    before = () =>
                    {
                        specification.comment = "I want to write specs";
                        specification.it["does something"] = () => { };
                        specification.Execute();
                    };

                    it["writes the comment into the log with the correct indentation"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "Specification"           + Environment.NewLine +
                            "  I want to write specs" + Environment.NewLine +
                            "  does something"
                        );
                    };
                };

                context["with a multi-line comment"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };

                    before = () =>
                    {
                        specification.comment = @"
                            as a programmer
                            I want to write specs
                            in order to improve my code quality
                        ";

                        specification.it["does something"] = () => { };
                        specification.Execute();
                    };

                    it["writes the comment into the log with the correct indentation"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "Specification" + Environment.NewLine +
                            "  as a programmer" + Environment.NewLine +
                            "  I want to write specs" + Environment.NewLine +
                            "  in order to improve my code quality" + Environment.NewLine +
                            "  does something"
                        );
                    };
                };

                context["with a comment after an assertion"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };

                    before = () =>
                    {
                        specification.comment = "comment before";
                        specification.it["does something"] = () => { };
                        specification.comment = "comment after";
                        specification.it["does something else"] = () => { };
                        specification.Execute();
                    };

                    it["writes the comment into the log with the correct indentation"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain(
                            "Specification" + Environment.NewLine +
                            "  comment before" + Environment.NewLine +
                            "  does something" + Environment.NewLine +
                            "  comment after" + Environment.NewLine +
                            "  does something else"
                        );
                    };
                };
            };
        }
Пример #6
0
        public CategorySpecification()
        {
            describe["Writing a specification"] = () =>
            {
                context["with a categorised assertion"] = () =>
                {
                    var specification = new MySpecification();

                    before = () =>
                    {
                        specification.@_["category"] =
                            specification.it["does something"] = null;
                    };

                    it["adds the category to the test definition"] = () =>
                    {
                        specification.Tests.Single().Properties[PropertyNames.Category].ShouldContain("category");
                    };
                };

                context["with a categorised context"] = () =>
                {
                    var specification = new MySpecification();

                    before = () =>
                    {
                        specification.@_["category"] =
                            specification.describe["something"] = () =>
                            {
                                specification.it["does something"] = null;
                            };
                    };

                    it["adds the category to the test definition"] = () =>
                    {
                        specification.Tests.Single().Properties[PropertyNames.Category].ShouldContain("category");
                    };
                };

                context["with multiple categories"] = () =>
                {
                    var specification = new MySpecification();

                    before = () =>
                    {
                        specification.@_["category 1", "category 2"] =
                            specification.describe["something"] = () =>
                            {
                                specification.@_["category 2", "category 3"] =
                                    specification.it["does something"] = null;
                            };
                    };

                    it["adds each category to the test definition"] = () =>
                    {
                        specification.Tests.Single().Properties[PropertyNames.Category].ShouldContain("category 1");
                        specification.Tests.Single().Properties[PropertyNames.Category].ShouldContain("category 2");
                        specification.Tests.Single().Properties[PropertyNames.Category].ShouldContain("category 3");
                    };


                    it["de-duplicates the categories"] = () =>
                    {
                        specification.Tests.Single().Properties[PropertyNames.Category].Cast<string>().Count(s => s == "category 2").ShouldEqual(1);
                    };
                };
            };
        }
Пример #7
0
        public PendingSpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with a pending assertion"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    AggregateException exception = null;

                    before = () =>
                    {
                        specification.it["has a pending assertion"] = null;
                        exception = Catch<AggregateException>(() => specification.Execute());
                    };

                    it["throws an inconclusive exception to the test runner"] = () =>
                    {
                        exception.InnerExceptions.Single().ShouldBeOfExactType<InconclusiveException>();
                    };

                    it["logs that the assertion is pending"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("has a pending assertion [PENDING]");
                    };
                };

                context["with a pending activity"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executed = false;
                    AggregateException exception = null;

                    before = () =>
                    {
                        specification.When["it has a pending assertion"] = null;
                        specification.it["still does something"] = () => executed = true;
                        exception = Catch<AggregateException>(() => specification.Execute());
                    };

                    it["executes the code in subsequent assertions"] = () =>
                    {
                        executed.ShouldBeTrue();
                    };

                    it["logs that the activity is pending"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("When it has a pending assertion [PENDING]");
                    };
                };
            };

            describe["Writing a specification"] = () =>
            {
                context["with a pending context"] = () =>
                {
                    var specification = new MySpecification();

                    before = () =>
                    {
                        specification.describe["pending context"] = null;
                    };

                    it["contains no tests"] = () =>
                    {
                        specification.Tests.ShouldBeEmpty();
                    };
                };
            };
        }
Пример #8
0
        public HookSpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with a setup step"] = () =>
                {
                    var specification = new MySpecification { Logger = null };
                    var executed = 0;

                    before = () =>
                    {
                        specification.before_each = () => executed++;
                        specification.before = () => { };
                        specification.it["does something"] = () => { };
                        specification.it["does something else"] = () => { };
                        specification.Execute();
                    };

                    it["runs the setup before each assertion"] = () =>
                    {
                        executed.ShouldEqual(2);
                    };
                };

                context["with a setup step before a context"] = () =>
                {
                    var specification = new MySpecification { Logger = null };
                    var executed = 0;

                    before = () =>
                    {
                        specification.before_each = () => executed++;
                        specification.describe["something"] = () =>
                        {
                            specification.it["does something"] = () => { };
                            specification.it["does something else"] = () => { };
                        };
                        specification.Execute();
                    };

                    it["runs the setup once before the assertions in the context"] = () =>
                    {
                        executed.ShouldEqual(1);
                    };
                };

                context["with a failing setup step"] = () =>
                {
                    var specification = new MySpecification { Logger = null };
                    var executed = false;
                    AggregateException exception = null;

                    before = () =>
                    {
                        specification.before_each = () => { throw new ArithmeticException(); };
                        specification.it["does something"] = () => executed = true;
                        exception = Catch<AggregateException>(() => specification.Execute());
                    };

                    it["skips the subsequent assertion"] = () =>
                    {
                        executed.ShouldBeFalse();
                    };

                    it["throws the exception"] = () =>
                    {
                        exception.InnerExceptions[0].ShouldBeOfExactType<ArithmeticException>();
                    };
                };
            };
        }
Пример #9
0
        public IgnoredSpecification()
        {
            describe["Executing a specification"] = () =>
            {
                context["with an ignored assertion"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executedA = false;
                    var executedB = false;
                    AggregateException exception = null;

                    before = () =>
                    {
                        specification.@ignore = 
                            specification.it["has an ignored assertion"] = () => executedA = true;
                        specification.it["has another assertion"] = () => executedB = true;

                        exception = Catch<AggregateException>(() => specification.Execute());
                    };

                    it["doesn't execute the code in the ignored assertion"] = () =>
                    {
                        executedA.ShouldBeFalse();
                    };

                    it["throws an ignored exception to the test runner"] = () =>
                    {
                        exception.InnerExceptions.Single().ShouldBeOfExactType<IgnoreException>();
                    };

                    it["executes the code in subsequent assertions"] = () =>
                    {
                        executedB.ShouldBeTrue();
                    };

                    it["logs that the assertion was ignored"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("has an ignored assertion [IGNORED]");
                    };
                };

                context["with an ignored activity"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executedA = false;
                    var executedB = false;

                    before = () =>
                    {
                        specification.@ignore =
                            specification.When["I ignore an activity"] = () => executedA = true;
                        specification.it["does something"] = () => executedB = true;

                        specification.Execute();
                    };

                    it["doesn't execute the code in the ignored activity"] = () =>
                    {
                        executedA.ShouldBeFalse();
                    };

                    it["executes the code in subsequent assertions"] = () =>
                    {
                        executedB.ShouldBeTrue();
                    };

                    it["logs that the activity was ignored"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("When I ignore an activity [IGNORED]");
                    };
                };

                context["with an ignored context"] = () =>
                {
                    var specification = new MySpecification { Logger = new StringWriter() };
                    var executed = false;
                    AggregateException exception = null;

                    before = () =>
                    {
                        specification.x =
                        specification.describe["something"] = () =>
                        {
                            specification.When["I do an activity"] = () => executed = true;
                            specification.it["makes an assertion"] = () => executed = true;
                        };

                        exception = Catch<AggregateException>(() => specification.Execute());
                    };

                    it["doesn't execute any steps inside the context"] = () =>
                    {
                        executed.ShouldBeFalse();
                    };

                    it["logs that the context was ignored"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("something [IGNORED]");
                    };

                    it["logs the activities in the context were ignored"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("When I do an activity [IGNORED]");
                    };

                    it["logs the assertions in the context were ignored"] = () =>
                    {
                        specification.Logger.ToString().ShouldContain("makes an assertion [IGNORED]");
                    };

                    it["throws an ignored exception to the test runner"] = () =>
                    {
                        exception.InnerExceptions.Single().ShouldBeOfExactType<IgnoreException>();
                    };
                };
            };
        }