Exemplo n.º 1
0
        public void AddsTagToFact()
        {
            var data    = new List <Fact>().AsQueryable();
            var mockSet = new Mock <DbSet <Fact> >();

            mockSet.As <IQueryable <Fact> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var optionsBuilder = new DbContextOptionsBuilder <FactOffContext>();

            _ = optionsBuilder.UseInMemoryDatabase();
            var  mockContext  = new FactOffContext(optionsBuilder.Options);
            var  factsService = new FactsService(mockContext);
            Fact fact         = new Fact();

            fact.Context = "This is a fact.";
            var factContext = "This is a fact.";

            fact.FactId = Guid.NewGuid();
            Tag tag = new Tag();

            tag.TagId = Guid.NewGuid();
            Guid  tagId   = tag.TagId;
            User  creator = new User();
            Theme theme   = new Theme()
            {
                Name = "Test theme"
            };

            factsService.CreateFact(factContext, creator, theme.Name);
            factsService.AddTag(fact, tag);

            Assert.AreEqual(fact.Tags.First().TagId, tagId, "TagId of added tag is not the one it should be.");
        }
Exemplo n.º 2
0
        public void GetFactByIdReturnsFact()
        {
            var data    = new List <Fact>().AsQueryable();
            var mockSet = new Mock <DbSet <Fact> >();

            mockSet.As <IQueryable <Fact> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var optionsBuilder = new DbContextOptionsBuilder <FactOffContext>();

            _ = optionsBuilder.UseInMemoryDatabase();
            var  mockContext  = new FactOffContext(optionsBuilder.Options);
            var  factsService = new FactsService(mockContext);
            Fact fact         = new Fact();

            fact.Context = "This is a fact.";
            Tag tag = new Tag();

            tag.TagId = Guid.NewGuid();
            Guid  tagId   = tag.TagId;
            User  creator = new User();
            Theme theme   = new Theme()
            {
                Name = "Test theme"
            };

            factsService.CreateFact(fact.Context, creator, theme.Name);
            factsService.AddTag(fact, tag);

            Assert.IsNull(factsService.GetFactById(tagId));
        }
Exemplo n.º 3
0
        public void RemoveTagFromDB()
        {
            var data    = new List <Fact>().AsQueryable();
            var mockSet = new Mock <DbSet <Fact> >();

            mockSet.As <IQueryable <Fact> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Fact> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var optionsBuilder = new DbContextOptionsBuilder <FactOffContext>();

            _ = optionsBuilder.UseInMemoryDatabase();
            var  mockContext  = new FactOffContext(optionsBuilder.Options);
            var  factsService = new FactsService(mockContext);
            Fact fact         = new Fact();

            fact.Context = "This is a fact.";
            Tag tag = new Tag();

            tag.Name  = "Test";
            tag.TagId = Guid.NewGuid();
            User  creator = new User();
            Theme theme   = new Theme()
            {
                Name = "Test theme"
            };

            factsService.CreateFact(fact.Context, creator, theme.Name);
            factsService.AddTag(fact, tag);
            if (mockContext.Facts.First() == fact)
            {
                factsService.RemoveTag(fact, tag);
            }

            Assert.AreEqual(mockContext.Facts.First().Tags.Count, 0, "The fact wasn't removed.");
        }