Commits

Christian Specht committed 6ed142c Draft

refactored tests for MissileSharp.Launcher
(mock CommandCenter --> makes testing the ViewModel much easier)

Comments (0)

Files changed (11)

src/MissileSharp.Launcher/MissileSharp.Launcher.csproj

       <SubType>Code</SubType>
     </Compile>
     <Compile Include="RelayCommand.cs" />
+    <Compile Include="Services\CommandCenterService.cs" />
     <Compile Include="Services\ConfigService.cs" />
+    <Compile Include="Services\ICommandCenterService.cs" />
     <Compile Include="Services\IConfigService.cs" />
     <Compile Include="Services\IMessageService.cs" />
     <Compile Include="Services\IShutdownService.cs" />

src/MissileSharp.Launcher/Services/CommandCenterService.cs

+using System.Configuration;
+
+namespace MissileSharp.Launcher.Services
+{
+    public class CommandCenterService : ICommandCenterService
+    {
+        public ICommandCenter GetCommandCenter()
+        {
+            string launcherName = ConfigurationManager.AppSettings["LauncherName"];
+            string launcherAssembly = ConfigurationManager.AppSettings["LauncherAssembly"];
+
+            var launcher = LauncherModelFactory.GetLauncher(launcherName, launcherAssembly);
+            return new CommandCenter(launcher);
+        }
+    }
+}

src/MissileSharp.Launcher/Services/ConfigService.cs

-using System.Configuration;
-using System.IO;
+using System.IO;
 using MissileSharp.Launcher.Properties;
 
 namespace MissileSharp.Launcher.Services
 
             return configFileLines;
         }
-
-        public string LauncherName
-        {
-            get { return ConfigurationManager.AppSettings["LauncherName"]; }
-        }
-
-        public string LauncherAssembly
-        {
-            get { return ConfigurationManager.AppSettings["LauncherAssembly"]; }
-        }
     }
 }

src/MissileSharp.Launcher/Services/ICommandCenterService.cs

+
+namespace MissileSharp.Launcher.Services
+{
+    public interface ICommandCenterService
+    {
+        ICommandCenter GetCommandCenter();
+    }
+}

src/MissileSharp.Launcher/Services/IConfigService.cs

     public interface IConfigService
     {
         string[] GetConfig();
-        string LauncherName { get; }
-        string LauncherAssembly { get; }
     }
 }

src/MissileSharp.Launcher/ViewModels/MainWindowViewModel.cs

 using System;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
-using System.Windows;
 using System.Windows.Input;
 using MissileSharp.Launcher.Properties;
 using MissileSharp.Launcher.Services;
 {
     public class MainWindowViewModel : INotifyPropertyChanged
     {
-        private CommandCenter model;
+        private ICommandCenter model;
         private IConfigService configService;
         private IMessageService messageService;
         private IShutdownService shutdownService;
 
         public event PropertyChangedEventHandler PropertyChanged;
 
-        public MainWindowViewModel(IConfigService configService, IMessageService messageService, IShutdownService shutdownService)
+        public MainWindowViewModel(ICommandCenterService commandCenterService, IConfigService configService, IMessageService messageService, IShutdownService shutdownService)
         {
             this.configService = configService;
             this.messageService = messageService;
             this.shutdownService = shutdownService;
-
-            var launcher = LauncherModelFactory.GetLauncher(this.configService.LauncherName, this.configService.LauncherAssembly);
-
-            this.model = new CommandCenter(launcher);
+            this.model = commandCenterService.GetCommandCenter();
 
             this.FireCommand = new RelayCommand(new Action<object>(this.FireMissile));
 

src/MissileSharp.Tests/Launcher/MainWindowViewModelTests.cs

-using System;
-using MissileSharp.Launcher.Services;
+using MissileSharp.Launcher.Services;
 using MissileSharp.Launcher.ViewModels;
 using NUnit.Framework;
 
     [TestFixture]
     public class MainWindowViewModelTests
     {
-        private MainWindowViewModel viewmodel;
         private MockShutdownService shutdownservice;
+        private ICommandCenterService commandcenterservice;
+        private MockCommandCenter commandcenter;
         
         [SetUp]
         public void Setup()
-        {
-            shutdownservice = new MockShutdownService();
-            viewmodel = new MainWindowViewModel(GetConfigService(), new StubMessageService(), shutdownservice);
+        {            
+            this.commandcenter = new MockCommandCenter();
+            this.commandcenterservice = new StubCommandCenterService();
+            ((StubCommandCenterService)commandcenterservice).CommandCenter = this.commandcenter;
+
+            this.shutdownservice = new MockShutdownService();
         }
 
-        public IConfigService GetConfigService(string[] config = null)
+        public MainWindowViewModel SetupViewModel(ICommandCenterService commandCenterService = null, IConfigService configService = null, IMessageService messageService = null, IShutdownService shutdownService = null)
         {
-            var configService = new StubConfigService();
-
-            if (config == null)
+            if (commandCenterService == null)
             {
-                configService.SetConfig(new string[] { "[name2]", "up,5", "[name1]", "up,5" });
-            }
-            else
-            {
-                configService.SetConfig(config);
+                commandCenterService = this.commandcenterservice;
             }
 
-            configService.LauncherAssembly = "MissileSharp.Tests.dll";
-            configService.LauncherName = "MissileSharp.Tests.StubMissileLauncher";
-            return configService;
+            if (configService == null)
+            {
+                configService = new StubConfigService();
+            }
+
+            if (messageService == null)
+            {
+                messageService = new StubMessageService();
+            }
+
+            if (shutdownService == null)
+            {
+                shutdownService = this.shutdownservice;
+            }
+
+            return new MainWindowViewModel(commandCenterService, configService, messageService, shutdownService);
         }
 
         [Test]
-        public void Constructor_ConfigWithTwoCommandSets_ObservableCollectionContainsTwoNames()
+        public void FireCommand_IsExecuted_RunCommandSetIsCalled()
         {
-            Assert.AreEqual(2, viewmodel.CommandSets.Count);
-        }
+            var viewmodel = SetupViewModel();
+            viewmodel.FireCommand.Execute("test");
 
-        [Test]
-        public void Constructor_ConfigWithTwoCommandSets_CollectionContainsOrderedCommandSets()
-        {
-            Assert.AreEqual("name1", viewmodel.CommandSets[0]);
-            Assert.AreEqual("name2", viewmodel.CommandSets[1]);
-        }
-
-        [Test]
-        public void Constructor_ConfigIsEmpty_ThrowsException()
-        {
-            var config = new StubConfigService();
-            Assert.Throws<ArgumentNullException>(() => new MainWindowViewModel(config, new StubMessageService(), shutdownservice));
-        }
-
-        [Test]
-        public void Constructor_LoadCommandSetsThrowsException_AppShutsDown()
-        {
-            // make model.LoadCommandSets throw by passing invalid config
-            var config = GetConfigService(new string[] { "invalid" });
-            new MainWindowViewModel(config, new StubMessageService(), shutdownservice);
-
-            Assert.True(shutdownservice.ShutDownWasCalled);
+            Assert.True(this.commandcenter.RunCommandSetWithStringWasCalled);
+            Assert.AreEqual("test", this.commandcenter.RunCommandSetCommandSetName);
         }
     }
 }

src/MissileSharp.Tests/Launcher/MockCommandCenter.cs

+using System;
+using System.Collections.Generic;
+
+namespace MissileSharp.Tests.Launcher
+{
+    public class MockCommandCenter : ICommandCenter
+    {
+        public bool IsReady
+        {
+            get { throw new NotImplementedException(); }
+        }
+
+        public void RunCommand(LauncherCommand command)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void RunCommandSet(IEnumerable<LauncherCommand> commands)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool RunCommandSetWithStringWasCalled { get; private set; }
+        public string RunCommandSetCommandSetName { get; private set; }
+
+        public void RunCommandSet(string commandSetName)
+        {
+            this.RunCommandSetWithStringWasCalled = true;
+            this.RunCommandSetCommandSetName = commandSetName;
+        }
+
+        public bool LoadCommandSets(string pathToConfigFile)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool LoadCommandSets(string[] configFileLines)
+        {
+            throw new NotImplementedException();
+        }
+
+        public List<string> GetLoadedCommandSetNames()
+        {
+            throw new NotImplementedException();
+        }
+
+        public ICommandCenter Up(int milliseconds)
+        {
+            throw new NotImplementedException();
+        }
+
+        public ICommandCenter Down(int milliseconds)
+        {
+            throw new NotImplementedException();
+        }
+
+        public ICommandCenter Left(int milliseconds)
+        {
+            throw new NotImplementedException();
+        }
+
+        public ICommandCenter Right(int milliseconds)
+        {
+            throw new NotImplementedException();
+        }
+
+        public ICommandCenter Reset()
+        {
+            throw new NotImplementedException();
+        }
+
+        public ICommandCenter Fire(int numberOfShots)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void Dispose()
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

src/MissileSharp.Tests/Launcher/StubCommandCenterService.cs

+using MissileSharp.Launcher.Services;
+
+namespace MissileSharp.Tests.Launcher
+{
+    public class StubCommandCenterService : ICommandCenterService
+    {
+        /// <summary>
+        /// set the command center that is returned
+        /// </summary>
+        public ICommandCenter CommandCenter { private get; set; }
+
+        public ICommandCenter GetCommandCenter()
+        {
+            return this.CommandCenter;
+        }
+    }
+}

src/MissileSharp.Tests/Launcher/StubConfigService.cs

         {
             return this.config;
         }
-
-        public string LauncherName { get; set; }
-
-        public string LauncherAssembly { get; set; }
     }
 }

src/MissileSharp.Tests/MissileSharp.Tests.csproj

       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath>
     </Reference>
+    <Reference Include="PresentationCore" />
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
     <Compile Include="..\MissileSharp\AssemblyVersion.cs">
       <Link>AssemblyVersion.cs</Link>
     </Compile>
+    <Compile Include="Launcher\MockCommandCenter.cs" />
+    <Compile Include="Launcher\StubCommandCenterService.cs" />
     <Compile Include="Launcher\MockShutdownService.cs" />
     <Compile Include="Launcher\StubConfigService.cs" />
     <Compile Include="Launcher\StubMessageService.cs" />