It's time to add some new functionality. Now that the add song window can be opened and closed, the next thing I can think of is to make sure the window is properly initialized. I start by adding a textbox for the song title and make sure that it is empty. To keep things simple, I postpone adding artist name and song length.
I start by adding a new test.
[Test] public void Add_new_song_window_is_propertly_initialized() { Application .Start(view => { view.ClickAddButton(addView => { Assert.That(addView.SongTitle, Is.Empty); addView.ClickCancelButton(); }); view.ClickCloseButton(); }); }To make the code compile, I add the SongTitle property to the view.
public class AddSongViewFake : IAddSongView { public string SongTitle { get; set; } // ... }Next, I run the test.
A very strange failure. But it is something I have seen before and the problem is that Is.Empty doesn't give good failure messages. So I change the assertion.
Assert.That(addView.SongTitle, Is.EqualTo( string .Empty));Now the failure is much better.
I was hoping for an empty string, but the song title was null. The failure message could still be improved, though, since there's nothing in it that indicates that it is the song title that was wrong. So I add a message to the assertion.
Assert.That(addView.SongTitle, Is.EqualTo( string .Empty), "SongTitle");Like so many times before, what I need to do first is to pull the SongTitle member into the interface so that the production code can access it, and then add it to the real view.
public interface IAddSongView { // ... string SongTitle { get; set; } } public partial class AddSongView : Form, IAddSongView { // ... public string SongTitle { get { return _songTitleTextBox.Text; } set { _songTitleTextBox.Text = value; } } // ... }Next I make the test pass.
public class AddSongShower { // ... public void Show() { View.SongTitle = string .Empty; View.OnCancelButtonClick += View.CloseView; View.ShowView(); } }As usual, there's only one line needed. The test is green, and running the application works fine, so I check in my code. Here is what the window looks like:
