Example #1
0
        public void FetchScriptById_Test()
        {
            //Arange
            var stubFactory = new Mock <IPrompterDbContextFactory>();
            var stubContext = new Mock <IPrompterDbContext>();
            var scriptDbSet = new Mock <IDbSet <Script> >();

            var fakeScripts = new List <Script>
            {
                new Script
                {
                    ScriptId = 1,
                    Sections = new List <Section>
                    {
                        new Section
                        {
                            Order = 2
                        },
                        new Section
                        {
                            Order = 1
                        }
                    }
                },
                new Script
                {
                    ScriptId = 2
                }
            }.AsQueryable();

            stubFactory.Setup(s => s.Create())
            .Returns(stubContext.Object);

            scriptDbSet.Setup(s => s.Provider)
            .Returns(fakeScripts.Provider);
            scriptDbSet.Setup(s => s.Expression)
            .Returns(fakeScripts.Expression);
            scriptDbSet.Setup(s => s.GetEnumerator())
            .Returns(fakeScripts.GetEnumerator());

            stubContext.Setup(s => s.Scripts)
            .Returns(scriptDbSet.Object);

            var service = new ScriptService(stubFactory.Object);

            //Act
            var actual     = service.FetchScriptById(1);
            var nullScript = service.FetchScriptById(15);

            //Assert
            Assert.That(nullScript, Is.Null);
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.ScriptId == 1);
            Assert.That(SectionsAreOrdered(actual), Is.True);
            stubContext.Verify(s => s.Scripts, Times.Exactly(2));
        }