Beispiel #1
0
        public GetAllTestsWithFakeObjects(ITestOutputHelper output)
        {
            this.output = output;

            var fakeContext = new Mock <ICrossroadsContext>();

            var fakeDbSet = new Mock <DbSet <TransactionType> >();

            this.data = new List <TransactionType>();
            this.data.Add(new TransactionType {
                Abbreviation = "N", BackOutType = false, Active = true, Name = "New Business", Id = 1
            });
            this.data.Add(new TransactionType {
                Abbreviation = "BN", BackOutType = true, Active = true, Name = "Backout of New Business", Id = 2
            });

            var source = this.data.AsQueryable();

            fakeDbSet.As <IQueryable <TransactionType> >().Setup(x => x.Provider).Returns(source.Provider);
            fakeDbSet.As <IQueryable <TransactionType> >().Setup(x => x.Expression).Returns(source.Expression);
            fakeDbSet.As <IQueryable <TransactionType> >().Setup(x => x.ElementType).Returns(source.ElementType);
            fakeDbSet.As <IQueryable <TransactionType> >().Setup(x => x.GetEnumerator()).Returns(source.GetEnumerator());

            fakeDbSet.Setup(x => x.AsNoTracking()).Returns(fakeDbSet.Object);

            fakeContext.Setup(x => x.TransactionTypes).Returns(fakeDbSet.Object);

            fakeDbSet.Setup(x => x.Add(It.IsAny <TransactionType>())).Returns((TransactionType x) =>
            {
                var maxId = this.data.Max(d => d.Id);

                x.Id = maxId + 1;

                return(x);
            });

            fakeContext.Setup(x => x.SaveChanges()).Returns(() =>
            {
                return(1);
            });


            this.context = fakeContext.Object;
        }
 public TransactionTypeRepository(ICrossroadsContext context)
 {
     this.context = context;
 }