InventoryCommandFactoryTests

InventoryCommandFactoryTests contains unit tests related to InventoryCommandFactory. Because each test will have a similar pattern of constructing InventoryCommandFactory and its IUserInterface dependency and then running the GetCommand method, a shared method is created that will run when the test initializes:

[TestInitialize]
public void Initialize()
{
var expectedInterface = new Helpers.TestUserInterface(
new List<Tuple<string, string>>(),
new List<string>(),
new List<string>()
);

Factory = new InventoryCommandFactory(expectedInterface);
}

The Initialize method constructs a stubbed IUserInterface and sets the Factory property. The individual unit tests then take a simple form of validating that the object returned is the correct type. First, an instance of the QuitCommand class should be returned when the user enters "q" or "quit", as follows:

[TestMethod]
public void QuitCommand_Successful()
{
Assert.IsInstanceOfType(Factory.GetCommand("q"), typeof(QuitCommand),
"q should be QuitCommand");
Assert.IsInstanceOfType(Factory.GetCommand("quit"), typeof(QuitCommand),
"quit should be QuitCommand");
}

The QuitCommand_Successful test method validates that when the InventoryCommandFactory method, GetCommand, is run, the object returned is a specific instance of the QuitCommand type. HelpCommand is only available when "?" is submitted:

[TestMethod]
public void HelpCommand_Successful()
{
Assert.IsInstanceOfType(Factory.GetCommand("?"), typeof(HelpCommand), "h should be HelpCommand");
}

The team did add a test for UnknownCommand that validated how InventoryCommand would respond when given a value not matching an existing command:

[TestMethod]
public void UnknownCommand_Successful()
{
Assert.IsInstanceOfType(Factory.GetCommand("add"), typeof(UnknownCommand),
"unmatched command should be UnknownCommand");
Assert.IsInstanceOfType(Factory.GetCommand("addinventry"), typeof(UnknownCommand),
"unmatched command should be UnknownCommand");
Assert.IsInstanceOfType(Factory.GetCommand("h"), typeof(UnknownCommand),
"unmatched command should be UnknownCommand");
Assert.IsInstanceOfType(Factory.GetCommand("help"), typeof(UnknownCommand),
"unmatched command should be UnknownCommand");
}

With the test methods in place, we can now cover a scenario where a command is given that does not match a known command in the application. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.119.148