コード例 #1
0
        public void UpdateSongTest()
        {
            Mock <ISongRepository>  songRepositoryMock  = new Mock <ISongRepository>();
            Mock <IAlbumRepository> albumRepositoryMock = new Mock <IAlbumRepository>();

            songRepositoryMock.Setup(s => s.Update(It.IsAny <Song>())).Callback <Song>(s =>
            {
                songs = songs.Where(s2 => s2.Id != s.Id).ToList();
                songs.Add(s);
            });
            songRepositoryMock.Setup(s => s.FindOne(1)).Returns(songs.Where(s => s.Id == 1).SingleOrDefault());

            albumRepositoryMock.Setup(a => a.FindOne(1)).Returns(new Album {
                Id = 1, Name = "TestAlbum"
            });

            ISongService songService = new SongService(songRepositoryMock.Object, albumRepositoryMock.Object);

            Song song = songService.FindById(1);

            song.Album = new Album {
                Id = 1
            };
            song.Title = "ChangedTitle";
            songService.Update(song);

            song = songService.FindById(1);
            Assert.AreEqual(song.Title, "ChangedTitle");
        }
コード例 #2
0
        public void DeleteSongTest()
        {
            Mock <ISongRepository> songRepositoryMock = new Mock <ISongRepository>();

            var songId1 = songs.Where(s => s.Id == 1).ToList();

            songRepositoryMock.Setup(s => s.Delete(It.IsAny <Song>())).Callback <Song>(s => songs.Remove(s));
            songRepositoryMock.Setup(s => s.FindOne(It.IsAny <long>())).Returns <long>(l => songs.Where(s => s.Id == l).SingleOrDefault());
            ISongService songService = new SongService(songRepositoryMock.Object, null);

            songService.Delete(1);
            Assert.AreEqual(null, songService.FindById(1));
            songService.Delete(1);
        }