public void Process_will_throw_if_an_null_task_is_passed()
        {
            _taskDispatcherMock
            .Setup(x => x.GetTask())
            .Returns((string)null);
            _instrumentMock
            .Setup(x => x.Execute(null))
            .Throws <ArgumentNullException>();

            Action action = () => _sut.Process();

            action.Should().Throw <ArgumentNullException>();
        }
        public void RequesTheNextTaskWhenProcessIsCalled()
        {
            Mock <ITaskDispatcher> taskDispatcherMock = new Mock <ITaskDispatcher>();
            Mock <IInstrument>     instrumentMock     = new Mock <IInstrument>();
            var instrumentProcessor = new InstrumentProcessor(taskDispatcherMock.Object, instrumentMock.Object);

            instrumentProcessor.Process();

            taskDispatcherMock.Verify(x => x.GetTask(), Times.Once());
        }
        public void CallGetTaskOnTheTaskDispatcher_WhenProcessingTask()
        {
            var taskDispatcher      = Substitute.For <ITaskDispatcher>();
            var instrument          = Substitute.For <IInstrument>();
            var instrumentProcessor = new InstrumentProcessor(taskDispatcher, instrument);

            instrumentProcessor.Process();

            taskDispatcher.Received(1).GetTask();
        }
        public void ThrowAnExceptionOnExecuteMethodToProcessCall()
        {
            Mock <ITaskDispatcher> taskDispatcherMock = new Mock <ITaskDispatcher>();
            Mock <IInstrument>     instrumentMock     = new Mock <IInstrument>();

            taskDispatcherMock.Setup(x => x.GetTask()).Throws <Exception>();

            var instrumentProcessor = new InstrumentProcessor(taskDispatcherMock.Object, instrumentMock.Object);

            Assert.Throws <Exception>(() => instrumentProcessor.Process());
        }
        public void CallTheInstrumentWithTheCorrectTask()
        {
            var taskDispatcher = new TaskDispatcherStub();

            var instrumentMock = new Mock <IInstrument>();

            var instrumentProcessor = new InstrumentProcessor(taskDispatcher, instrumentMock.Object);

            instrumentProcessor.Process();

            instrumentMock.Verify(x => x.Execute("stub test"));
        }
        public void when_the_Execute_method_of_the_instrument_throws_an_exception_then_this_exception_is_passed_on_to_the_caller_of_the_Process_method()
        {
            ////Arrange
            var instrument = new Mock<IInstrument>();
            var taskDispatcher = new Mock<ITaskDispatcher>();
            var instrumentProcessor = new InstrumentProcessor(instrument.Object, taskDispatcher.Object, new Mock<IConsole>().Object);
            instrument.Setup(i => i.Execute(It.IsAny<string>())).Throws(new Exception());

            ////Act
            instrumentProcessor.Process();

            ////Assert
        }
        public void CallExecuteOnAnInstrumentToProcessATaskWhenProcessMethodIsCalled()
        {
            Mock <ITaskDispatcher> taskDispatcherMock = new Mock <ITaskDispatcher>();
            Mock <IInstrument>     instrumentMock     = new Mock <IInstrument>();

            taskDispatcherMock.Setup(x => x.GetTask()).Returns("");

            var instrumentProcessor = new InstrumentProcessor(taskDispatcherMock.Object, instrumentMock.Object);

            instrumentProcessor.Process();

            instrumentMock.Verify(x => x.Execute(string.Empty), Times.Once());
        }
        public void ReturnTaskFinished_WhenProcessing_ASuccessfulTask()
        {
            var taskDispatcher = Substitute.For <ITaskDispatcher>();

            taskDispatcher.GetTask().Returns("Success");
            var instrument = Substitute.For <IInstrument>();

            instrument.When(i => i.Execute("Success")).Do(i => instrument.Finished += Raise.EventWith(new InstrumentProcessEventArgs("Success")));
            var instrumentProcessor = new InstrumentProcessor(taskDispatcher, instrument);

            instrumentProcessor.Process();

            instrument.Received(1).Execute("Success");
        }
        public void RaiseErrorEvent_WhenProcessing_AnTaskWhichShouldError()
        {
            var taskDispatcher = Substitute.For <ITaskDispatcher>();

            taskDispatcher.GetTask().Returns("ShouldError");
            var instrument = Substitute.For <IInstrument>();

            instrument.When(i => i.Execute("ShouldError")).Do(i => instrument.Error += Raise.EventWith(new InstrumentProcessEventArgs("ShouldError")));
            var instrumentProcessor = new InstrumentProcessor(taskDispatcher, instrument);

            instrumentProcessor.Process();

            instrument.Received(1).Execute("ShouldError");
        }
        public void ThrowANullReferenceException_WhenGivenANullTask()
        {
            var taskDispatcher = Substitute.For <ITaskDispatcher>();

            taskDispatcher.GetTask().Returns((string)null);
            var instrument = Substitute.For <IInstrument>();

            instrument.When(x => x.Execute(null)).Do(x => throw new NullReferenceException());
            var instrumentProcessor = new InstrumentProcessor(taskDispatcher, instrument);

            Action process = () => instrumentProcessor.Process();

            process.Should().Throws <ArgumentNullException>();
        }
        public void CallTheTaskDispatcherFinishedTaskWithTheCorrectTask()
        {
            var testTask           = "stub test";
            var taskDispatcherMock = new Mock <ITaskDispatcher>();

            taskDispatcherMock.Setup(x => x.GetTask()).Returns(testTask);

            var instrumentStub = new InstrumentStub();

            var instrumentProcessor = new InstrumentProcessor(taskDispatcherMock.Object, instrumentStub);

            instrumentProcessor.Process();

            taskDispatcherMock.Verify(x => x.FinishedTask(testTask));
        }
        public void CallTaskDispatcher_WithCorrectTaskName_WhenSuccessfullyFinishingATask()
        {
            var taskDispatcher = Substitute.For <ITaskDispatcher>();

            taskDispatcher.GetTask().Returns("GoodTask");
            var instrument = Substitute.For <IInstrument>();

            instrument.When(i => i.Execute("GoodTask")).Do(i => instrument.Finished += Raise.EventWith(new InstrumentProcessEventArgs("GoodTask")));
            var instrumentProcessor = new InstrumentProcessor(taskDispatcher, instrument);

            instrumentProcessor.Process();

            instrument.Received(1).Execute("GoodTask");
            taskDispatcher.Received(1).FinishedTask("GoodTask");
        }
        public void when_the_method_Process_is_called_then_the_InstrumentProcessor_gets_the_next_task_from_the_task_dispatcher_and_executes_it_on_the_instrument()
        {
            ////Arrange
            var instrument = Substitute.For<IInstrument>();
            var taskDispatcher = Substitute.For<ITaskDispatcher>();
            var instrumentProcessor = new InstrumentProcessor(instrument, taskDispatcher, Substitute.For<IConsole>());
            var message = Guid.NewGuid().ToString();
            taskDispatcher.GetTask().Returns(message);

            ////Act
            instrumentProcessor.Process();

            ////Assert
            instrument.Received().Execute(message);
        }
        public void when_the_instrument_fires_the_Finished_event_then_the_InstrumentProcessor_calls_the_task_dispatchers_FinishedTask_method_with_the_correct_task()
        {
            ////Arrange
            var instrument = new Mock<IInstrument>();
            var taskDispatcher = new Mock<ITaskDispatcher>();
            var instrumentProcessor = new InstrumentProcessor(instrument.Object, taskDispatcher.Object, new Mock<IConsole>().Object);
            var message = Guid.NewGuid().ToString();
            taskDispatcher.Setup(d => d.GetTask()).Returns(message);
            instrument.Setup(i => i.Execute(message)).Raises(i => i.Finished += null, EventArgs.Empty);

            ////Act
            instrumentProcessor.Process();

            ////Assert
            taskDispatcher.Verify(t => t.FinishedTask(message), Times.Once());
        }
        public void when_the_instrument_fires_the_Error_event_then_the_InstrumentProcessor_writes_the_string_Error_occurred_to_the_console()
        {
            ////Arrange
            var instrument = new Mock<IInstrument>();
            var taskDispatcher = new Mock<ITaskDispatcher>();
            var console = new Mock<IConsole>();
            var instrumentProcessor = new InstrumentProcessor(instrument.Object, taskDispatcher.Object, console.Object);
            var message = Guid.NewGuid().ToString();
            taskDispatcher.Setup(d => d.GetTask()).Returns(message);
            instrument.Setup(i => i.Execute(message)).Raises(i => i.Error += null, EventArgs.Empty);

            ////Act
            instrumentProcessor.Process();

            ////Assert
            console.Verify(c => c.WriteLine(message));
        }
        public void OnSuccess_InvokeFinishTask()
        {
            var taskDispatcher      = new Mock <ITaskDispatcher>();
            var instrument          = new Mock <IInstrument>();
            var instrumentProcessor = new InstrumentProcessor(taskDispatcher.Object, instrument.Object);

            taskDispatcher
            .Setup(theTaskDispatcher => theTaskDispatcher.GetTask())
            .Returns("task 1");
            instrument
            .Setup(theInstrument => theInstrument.Execute("task 1"))
            .Raises(theInstrument => theInstrument.Finished += null, new FinishedEventArgs("task 1"));


            instrumentProcessor.Process();


            taskDispatcher.Verify(theTaskDispatcher => theTaskDispatcher.FinishedTask("task 1"), Times.Once());
        }
        public void when_the_instrument_fires_the_Finished_event_then_the_InstrumentProcessor_calls_the_task_dispatchers_FinishedTask_method_with_the_correct_task_multiple()
        {
            ////Arrange
            var instrument = new Mock<IInstrument>();
            var taskDispatcher = new Mock<ITaskDispatcher>();
            var instrumentProcessor = new InstrumentProcessor(instrument.Object, taskDispatcher.Object, new Mock<IConsole>().Object);

            ////Act
            var messages = new Queue<string>();
            for (int i1 = 0; i1 < 10; i1++)
            {
                var message = Guid.NewGuid().ToString();
                messages.Enqueue(message);
                taskDispatcher.Setup(d => d.GetTask()).Returns(message);
                instrumentProcessor.Process();
            }

            ////Assert
            for (int i2 = 0; i2 < 10; i2++)
            {
                instrument.Raise(i => i.Finished += null, EventArgs.Empty);
                ////TODO: Hm, doesn't work inline...
                var message = messages.Dequeue();
                taskDispatcher.Verify(t => t.FinishedTask(message), Times.Once());
            }
        }
        public void when_the_method_Process_is_called_then_the_InstrumentProcessor_gets_the_next_task_from_the_task_dispatcher_and_executes_it_on_the_instrument()
        {
            ////Arrange
            var instrument = new Mock<IInstrument>();
            var taskDispatcher = new Mock<ITaskDispatcher>();
            var instrumentProcessor = new InstrumentProcessor(instrument.Object, taskDispatcher.Object, new Mock<IConsole>().Object);
            var message = Guid.NewGuid().ToString();
            taskDispatcher.Setup(d => d.GetTask()).Returns(message);

            ////Act
            instrumentProcessor.Process();

            ////Assert
            instrument.Verify(i => i.Execute(message), Times.Once());
        }
        public void request_a_task_from_despatcher_when_processing()
        {
            _instrumentProcessor.Process();

            _taskDispatcher.Verify(t => t.GetTask());
        }