示例#1
0
        public void SaveCard(Card card)
        {
            card.Question = card.Question.Trim();
            card.Answer = card.Answer.Trim();

            if (card.Id == 0) {
                context.Cards.Add(card);
            } else {
                Card dbEntry = context.Cards.Find(card.Id);
                if (dbEntry != null) {
                    dbEntry.Question = card.Question;
                    dbEntry.Answer = card.Answer;
                    dbEntry.Note = card.Note;
                    dbEntry.CreatedAt = card.CreatedAt;
                    dbEntry.ReviewedAt = card.ReviewedAt;
                    dbEntry.ReviewLevel = card.ReviewLevel;
                }
            }
            context.SaveChanges();
        }
        public void BeginTestMethod()
        {
            TestHelper.SetUser(ctrlContext, userId: 1, userRole: "Administrator");

            var cards = new Card[] {
                new Card {
                    Id = 1,
                    UserId = 2,
                    Question = "Why don't you try it?",
                    Answer = "a1",
                    CreatedAt = DateTime.Parse("2014/01/01")
                },
                new Card {
                    Id = 2,
                    UserId = 2,
                    Question = "Even though it's difficult, it's worth trying.",
                    Answer = "a2",
                    CreatedAt = DateTime.Parse("2014/01/02")
                },
                new Card {
                    Id = 3,
                    UserId = 2,
                    Question = "What a wonderful day!",
                    Answer = "a3",
                    CreatedAt = DateTime.Parse("2014/01/03")
                },
                new Card {
                    Id = 4,
                    UserId = 2,
                    Question = "It's only 1.2$.",
                    Answer = "a4",
                    CreatedAt = DateTime.Parse("2014/01/04")
                },
                new Card {
                    Id = 5,
                    UserId = 2,
                    Question = "2 * 3 + 6 / 2 = 9",
                    Answer = "a5",
                    CreatedAt = DateTime.Parse("2014/01/05")
                },
                new Card {
                    Id = 6,
                    UserId = 3,
                    Question = "question1",
                    Answer = "answer1",
                    CreatedAt = DateTime.Parse("2014/01/06")
                },
                new Card {
                    Id = 7,
                    UserId = 3,
                    Question = "question2",
                    Answer = "answer2",
                    CreatedAt = DateTime.Parse("2014/01/07")
                }
            }.OrderByDescending(c => c.CreatedAt)
            .AsQueryable();

            mockRepository.Setup(m => m.Cards).Returns(cards);

            mockRepository.Setup(m => m.DeleteCard(It.IsAny<int>()))
                .Callback((int id) => mockRepository.Object.Cards.ToList().Remove(cards.ToList().Find(c => c.Id == id)))
                .Returns((int id) => cards.ToList().Find(c => c.Id == id));
        }