Thought Cloud TDD #1: Initial thoughts

I’m building a demo for Correspondence. Grab the Thought Cloud code and follow along.

The UnitTest project starts out with a model test, but I want to build this thing view model first. So let’s initialize a new test class.

[TestClass]
public class ViewModelTest : SilverlightTest
{
    private Community _community;
    private Identity _identity;
    private CloudViewModel _cloudViewModel;

    [TestInitialize]
    public void Initialize()
    {
        _community = new Community(new MemoryStorageStrategy())
            .Register<Model.CorrespondenceModel>();

        _identity = _community.AddFact(new Identity("mike"));
        _cloudViewModel = new CloudViewModel();
    }
}

The Community uses the memory storage strategy, which is ideal for unit testing. The real application will use isolated storage, but memory will be reset with each test run. We also register the model, and create the initial fact, and create a new view model.

When the user first starts the application, they should see a thought bubble that says “My thought”. (Actually, first they must log in, but that is uninteresting to me right now.)

[TestMethod]
public void InitialThoughtIsMyThought()
{
    ThoughtViewModel thought = _cloudViewModel.Thoughts.Single();
    Assert.AreEqual("My thought", thought.Text);
}

To make this pass, we do the simplest thing possible.

public class CloudViewModel
{
    public IEnumerable<ThoughtViewModel> Thoughts
    {
        get
        {
            yield return new ThoughtViewModel();
        }
    }
}
public class ThoughtViewModel
{
    public string Text
    {
        get
        {
            return "My thought";
        }
    }
}

Test passes. On to the next. The user can edit this initial thought to change the text.

[TestMethod]
public void CanChangeThoughtText()
{
    ThoughtViewModel thought = _cloudViewModel.Thoughts.Single();
    thought.Text = "New thought";
    Assert.AreEqual("New thought", thought.Text);
}

The simplest thing possible won’t make this pass anymore. By forcing ourselves to write simple methods, we ensure that there are no missing tests. We’ll make this test pass next time.

Leave a Reply

You must be logged in to post a comment.