示例#1
0
        public void CartManger_Calculate_Total_Value_And_Quantity_Works_Correctly()
        {
            List <CartItem>        mySession      = new List <CartItem>();
            Mock <ISessionManager> mockSession    = new Mock <ISessionManager>();
            Mock <ISongRepository> mockRepository = new Mock <ISongRepository>();

            mockSession.Setup(x => x.Set <List <CartItem> >(It.IsAny <string>(),
                                                            It.IsAny <List <CartItem> >())).Callback((string e, List <CartItem> s) => mySession = s);

            mockSession.Setup(x => x.Get <List <CartItem> >(It.IsAny <string>())).Returns(mySession);

            mockRepository.Setup(x => x.Songs).Returns(new Song[] { new Song()
                                                                    {
                                                                        SongId = 1, NameSong = "Test1", Price = 20
                                                                    },
                                                                    new Song()
                                                                    {
                                                                        SongId = 2, NameSong = "Test2", Price = 30
                                                                    },
                                                                    new Song()
                                                                    {
                                                                        SongId = 3, NameSong = "Test3", Price = 40
                                                                    },
                                                                    new Song()
                                                                    {
                                                                        SongId = 4, NameSong = "Test4", Price = 50
                                                                    } });

            CartManager target = new CartManager(mockSession.Object, mockRepository.Object);

            target.Add(1);
            target.Add(2);
            target.Add(1);
            target.Add(3);

            Assert.AreEqual(110, target.CalculateTotalValue());
            Assert.AreEqual(4, target.CalculateTotalQuantity());
        }