コード例 #1
0
        public void WhenQuestionnaireIsCompleted_ThenCanSubmitReturnsTrue()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            viewModel.Questionnaire.Name = "QuestionnaireName";
            viewModel.Questionnaire.Age = 5;
            AnswerAllQuestions(viewModel);

            // Assertions
            Assert.IsTrue(viewModel.CanSubmit);
        }
コード例 #2
0
        public void WhenQuestionnaireIsCreatedWithService_ThenRetrievesQuestionnaireFromService()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);
            var changeTracker = new PropertyChangeTracker(viewModel);

            Assert.AreEqual("Loading", viewModel.CurrentState);
            Assert.AreEqual(0, changeTracker.ChangedProperties.Count());

            callback(CreateQuestionnaireResult());

            // Assertions
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("Questionnaire"));
            Assert.AreEqual("Normal", viewModel.CurrentState);
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("CurrentState"));
            CollectionAssert.AreEqual(
                new[] { "Enter your name", "Enter your address" },
                viewModel.Questions.Cast<OpenQuestionViewModel>().Select(q => q.Question.QuestionText).ToArray());
        }
コード例 #3
0
        public void WhenQuestionHasFormattingErrorAfterHavingValidResponse_ThenViewModelCannotBeSubmitted()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult(new Questionnaire(new NumericQuestion("unknown"))));
            var questionViewModel = ((NumericQuestionViewModel)viewModel.Questions[0]);

            viewModel.Questionnaire.Name = "name";
            viewModel.Questionnaire.Age = 25;
            questionViewModel.Question.Response = 100;

            Assert.IsTrue(viewModel.CanSubmit);

            questionViewModel.HasBindingError = true;

            Assert.IsFalse(viewModel.CanSubmit);
        }
コード例 #4
0
        public void WhenQuestionnaireHasUnknownQuestion_ThenAnExceptionIsThrown()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            try
            {
                callback(CreateQuestionnaireResult(new Questionnaire(new MockQuestion("unknown"))));
                Assert.Fail("Expected ArgumentException");
            }
            catch (ArgumentException)
            {
                // expected exception
            }
        }
コード例 #5
0
        public void WhenQuestionnaireHasNumericQuestion_ThenTheMatchingViewModelIsCreated()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult(new Questionnaire(new NumericQuestion("Numeric"))));

            // Assertions
            Assert.IsInstanceOfType(viewModel.Questions[0], typeof(NumericQuestionViewModel));
        }
コード例 #6
0
        public void AfterSubmittingQuestionnaire_ThenChangeDataRelatedNotificationsAreFired()
        {
            Action<IOperationResult<Questionnaire>> callback = null;
            Action<IOperationResult> submitCallback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);
            repository.Setup(r => r.SubmitQuestionnaireAsync(It.IsAny<Questionnaire>(), It.IsAny<Action<IOperationResult>>()))
                .Callback<Questionnaire, Action<IOperationResult>>(
                (q, sc) => submitCallback = sc);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var tracker = new PropertyChangeTracker(viewModel);
            viewModel.Questionnaire.Name = "TestName";
            viewModel.Questionnaire.Age = 25;
            AnswerAllQuestions(viewModel);

            viewModel.Submit();
            submitCallback(new Mock<IOperationResult>().Object);
            callback(CreateQuestionnaireResult());

            Assert.IsTrue(tracker.ChangedProperties.Contains("Questionnaire"));
        }
コード例 #7
0
        public void AfterResetting_ThenANewQuestionnaireIsCreated()
        {
            QuestionViewModel[] originalQuestions = null;
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);
            callback(CreateQuestionnaireResult());

            originalQuestions = viewModel.Questions.ToArray();

            viewModel.Questionnaire.Name = "TestName";
            viewModel.Questionnaire.Age = 14;
            AnswerAllQuestions(viewModel);

            viewModel.Reset();

            var tracker = new PropertyChangeTracker(viewModel);
            callback(CreateQuestionnaireResult());

            CollectionAssert.AreNotEquivalent(originalQuestions, viewModel.Questions.ToArray());
            CollectionAssert.Contains(tracker.ChangedProperties, "CanSubmit");
        }
コード例 #8
0
        public void WhenAgeIsSetOnTheModel_ThenTheHasErrorPropertyInTheViewModelIsClearedAndSinglePropertyChangeNotificationIsFired()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult(new Questionnaire { }));

            viewModel.Questionnaire.Age = 25;
            viewModel.Questionnaire.Name = "name";

            Assert.IsTrue(viewModel.CanSubmit);

            viewModel.HasAgeBindingError = true;

            Assert.IsFalse(viewModel.CanSubmit);
        }
コード例 #9
0
        public void WhenQuestionnaireIsSubmitted_ThenSubmitsToService()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            repository.Setup(r => r.SubmitQuestionnaireAsync(It.IsAny<Questionnaire>(), It.IsAny<Action<IOperationResult>>()))
                .Verifiable("SubmitQuestionnaireAsync not called");

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            viewModel.Questionnaire.Name = "Name";
            viewModel.Questionnaire.Age = 21;
            AnswerAllQuestions(viewModel);
            viewModel.Submit();

            repository.Verify();
        }
コード例 #10
0
        public void WhenCurrentStateIsModified_ThenViewModelNotifiesUpdate()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var changeTracker = new PropertyChangeTracker(viewModel);

            viewModel.CurrentState = "newState";

            // Assertions
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("CurrentState"));
        }
コード例 #11
0
        public void WhenQuestionIsAnswered_ThenQuestionnaireModelNotifiesUpdate()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var question = viewModel.Questions.First() as OpenQuestionViewModel;
            var changeTracker = new PropertyChangeTracker(viewModel);
            question.Question.Response = "some text";

            // Assertions
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("UnansweredQuestions"));
        }
コード例 #12
0
        public void WhenQuestionIsAnswered_ThenAllUnansweredQuestionsAreUpdated()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var question = viewModel.Questions.First() as OpenQuestionViewModel;
            question.Question.Response = "some text";

            // Assertions
            Assert.AreEqual(2, viewModel.TotalQuestions);
            Assert.AreEqual(1, viewModel.UnansweredQuestions);
        }
コード例 #13
0
        public void WhenQuestionnaireIsCreated_ThenAllQuestionsAreUnanswered()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            // Assertions
            Assert.AreEqual(2, viewModel.TotalQuestions);
            Assert.AreEqual(2, viewModel.UnansweredQuestions);
        }
コード例 #14
0
        public void WhenGettingCalculatedPropertiesBeforeGettingAQuestionnaire_ThenReturnsDefaultValues()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            Assert.AreEqual(0, viewModel.TotalQuestions);
            Assert.AreEqual(0, viewModel.UnansweredQuestions);
            Assert.IsFalse(viewModel.CanSubmit);
        }
コード例 #15
0
        public void WhenServiceIsDoneSubmitting_ThenStateIsSetToNormal()
        {
            Action<IOperationResult<Questionnaire>> callback = null;
            Action<IOperationResult> submitCallback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);
            repository.Setup(r => r.SubmitQuestionnaireAsync(It.IsAny<Questionnaire>(), It.IsAny<Action<IOperationResult>>()))
                .Callback<Questionnaire, Action<IOperationResult>>(
                (q, sc) => submitCallback = sc);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            viewModel.Submit();
            Assert.AreEqual("Submitting", viewModel.CurrentState);

            var tracker = new PropertyChangeTracker(viewModel);

            // Responds as the service would.
            submitCallback(new Mock<IOperationResult>().Object);
            callback(CreateQuestionnaireResult());

            Assert.AreEqual("Normal", viewModel.CurrentState);
            CollectionAssert.Contains(tracker.ChangedProperties, "CanSubmit");
        }
コード例 #16
0
 private static void AnswerAllQuestions(QuestionnaireViewModel viewModel)
 {
     foreach (OpenQuestionViewModel questionViewModel in viewModel.Questions)
     {
         questionViewModel.Question.Response = "response";
     }
 }
コード例 #17
0
        public void AfterSubmitting_ANewQuestionnaireIsCreated()
        {
            QuestionViewModel[] originalQuestions = null;
            Action<IOperationResult<Questionnaire>> callback = null;
            Action<IOperationResult> submitCallback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);
            repository.Setup(r => r.SubmitQuestionnaireAsync(It.IsAny<Questionnaire>(), It.IsAny<Action<IOperationResult>>()))
                .Callback<Questionnaire, Action<IOperationResult>>(
                (q, sc) => submitCallback = sc);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            originalQuestions = viewModel.Questions.ToArray();

            viewModel.Questionnaire.Name = "TestName";
            viewModel.Questionnaire.Age = 14;
            AnswerAllQuestions(viewModel);

            viewModel.Submit();
            submitCallback(new Mock<IOperationResult>().Object);
            callback(CreateQuestionnaireResult());

            CollectionAssert.AreNotEquivalent(originalQuestions, viewModel.Questions.ToArray());
        }
コード例 #18
0
        public void WhenNameIsSet_ThenPropertyChangeOnCanSubmitIsFired()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult(new Questionnaire { }));

            int canSubmitPropertyChanges = 0;
            viewModel.PropertyChanged += (s, e) => { if (e.PropertyName == "CanSubmit") canSubmitPropertyChanges++; };

            viewModel.Questionnaire.Name = "name";

            Assert.AreEqual(1, canSubmitPropertyChanges);
        }
コード例 #19
0
        public void WhenHasErrorIsChangedToFalseOnTheViewModel_ThenPropertyChangeOnCanSubmitIsFired()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult(new Questionnaire { }));

            viewModel.HasAgeBindingError = true;
            int canSubmitPropertyChanges = 0;
            viewModel.PropertyChanged += (s, e) => { if (e.PropertyName == "CanSubmit") canSubmitPropertyChanges++; };
            viewModel.HasAgeBindingError = false;

            // Assertions
            Assert.AreEqual(1, canSubmitPropertyChanges);
        }