Beginner's Quickstart

Prepare for a crash-course introduction to SpecsFor! This article will show you how to create a project, add SpecsFor, and create your first spec!

📘

Newbie Content!

This quickstart is written for newbies! If you are comfortable with creating projects and adding NuGet packages, you should check out the Advanced Quickstart!

Create a project

Let's assume you've already got a solution, and you want to start writing specs for it! In fact, let's assume you're going to write unit specs AND integration specs! Open that solution, then add a new Class Library project to it!

First, let's create a solution folder (right-click on the solution, and choose "New Solution Folder") named Specs.

Next, right-click on your Specs folder, and choose "Add New Project." In the dialog that appears, find the class library project type:

955955

The name of your project isn't too important, but I typically use the pattern "SolutionName.UnitSpecs" and "SolutionName.IntegrationSpecs."

Repeat this process for your other spec project. Your solution should now have two spec projects:

315315

Now we're ready to add SpecsFor!

Add NuGet package

SpecsFor is available as a NuGet package. Let's go ahead and add it to your new spec project!

You could use the Visual Studio NuGet Package Manager Console, or you can use the GUI. Let's use the Package Manager Console.

In Visual Studio, go to the View menu, then select "Other Windows," then select "Package Manager Console."

652652

This will open the Package Manager Console within Visual Studio:

806806

Let's install SpecsFor into each of our spec projects. Run this command, and specify the full name of your spec project each time:

#Don't forget to change "SampleApplication1.UnitSpecs" to the name of your spec project!
Install-Package SpecsFor -ProjectName SampleApplication1.UnitSpecs
613613

After some churn, you should have SpecsFor installed into both projects. Over in Solution Explorer, expand each spec project, then expand their References folders. Each folder should contain a reference to SpecsFor:

247247

And there you have it! Both projects are now ready!

📘

Use ReSharper?

If you use ReSharper, be sure you check out the special template package for SpecsFor! You can find more information in this section: ReSharper Templates

Create your first spec!

You've got all the pieces you need, so let's write your first spec! Create a new class named SampleSpecs, and drop in this implementation:

public class SimpleSpecs
    {
        public class WidgetFactory
        {
            public Widget CreateWidget(string color, int size)
            {
                return new Widget
                {
                    Color = color
                };
            }
        }

        public class Widget
        {
            public string Color { get; set; }
            public int Size { get; set; }
        }

        public class when_creating_a_widget : SpecsFor<WidgetFactory>
        {
            private Widget _widget;

            protected override void When()
            {
                _widget = SUT.CreateWidget(color: "Red", size: 500);
            }

            [Test]
            public void then_it_sets_the_color()
            {
                _widget.Color.ShouldEqual("Red");
            }

            [Test]
            public void then_it_sets_the_size()
            {
                _widget.Size.ShouldEqual(500);
            }
        }
    }

This snippet contains a couple of Widget-related classes and a simple spec. Run the specs with your favorite test runner. You should have one failing spec, and one passing spec! There's a small bug in the WidgetFactory! Let's fix it! Here's the new, corrected, factory:

public class WidgetFactory
        {
            public Widget CreateWidget(string color, int size)
            {
                return new Widget
                {
                    Color = color,
                    Size = size
                };
            }
        }

Congratulations! You now have a simple, passing spec! You're ready to start writing specs of your own! Continue your adventure by Creating a basic spec!


Did this page help you?