public void Insert() { var now = DateTime.UtcNow; var generator = new RandomObjectGenerator(); var userId = generator.Generate<int>(); var poll = EntityCreator.Create<MVPoll>(_ => { _.PollDeletedFlag = false; _.PollStartDate = now.AddDays(-2); _.PollEndDate = now.AddDays(2); _.PollMinAnswers = 2; _.PollMaxAnswers = 3; }); poll.MVPollOptions = new List<MVPollOption> { EntityCreator.Create<MVPollOption>(), EntityCreator.Create<MVPollOption>(), EntityCreator.Create<MVPollOption>(), EntityCreator.Create<MVPollOption>() }; var category = EntityCreator.Create<MVCategory>(_ => _.CategoryID = poll.PollCategoryID); var polls = new InMemoryDbSet<MVPoll> { poll }; var submissions = new InMemoryDbSet<MVPollSubmission>(); var responses = new InMemoryDbSet<MVPollResponse>(); var saveChangesCount = 0; var submissionId = generator.Generate<int>(); var entities = new Mock<IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls).Returns(polls); entities.Setup(_ => _.MVPollSubmissions).Returns(submissions); entities.Setup(_ => _.MVPollResponses).Returns(responses); entities.Setup(_ => _.MVCategories) .Returns(new InMemoryDbSet<MVCategory> { category }); entities.Setup(_ => _.SaveChanges()).Callback(() => { if (saveChangesCount == 0) { submissions.Local[0].PollSubmissionID = submissionId; } saveChangesCount++; }).Returns(1); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register<IEntities>(_ => entities.Object); using (new ObjectActivator(builder.Build()).Bind(() => ApplicationContext.DataPortalActivator)) { var criteria = new PollSubmissionCriteria(poll.PollID, userId); var submission = new DataPortal<PollSubmission>().Create(criteria); submission.Responses[1].IsOptionSelected = true; submission.Responses[3].IsOptionSelected = true; submission = submission.Save(); Assert.AreEqual(1, submissions.Local.Count, submissions.GetPropertyName(_ => _.Local.Count)); Assert.AreEqual(submissionId, submission.PollSubmissionID, submission.GetPropertyName(_ => _.PollSubmissionID)); Assert.AreEqual(4, responses.Local.Count, responses.GetPropertyName(_ => _.Local.Count)); Assert.IsFalse(responses.Local[0].OptionSelected, responses.GetPropertyName(_ => _.Local[0].OptionSelected)); Assert.IsTrue(responses.Local[1].OptionSelected, responses.GetPropertyName(_ => _.Local[1].OptionSelected)); Assert.IsFalse(responses.Local[2].OptionSelected, responses.GetPropertyName(_ => _.Local[2].OptionSelected)); Assert.IsTrue(responses.Local[3].OptionSelected, responses.GetPropertyName(_ => _.Local[3].OptionSelected)); } entities.VerifyAll(); }