public void PostResponse()
        {
            // DBのモックを用意する
            var mockposts = new Mock<ICollection<BoardPostEntity>>();
            var mockset = new Mock<DbSet<BoardEntity>>();
            var mockcontext = new Mock<BoardDbContext>();

            var originalData = new List<BoardEntity> {
                new BoardEntity { Id = 1, Title = "A", Text = "a", Posts = mockposts.Object },
            };
            var data = originalData.AsQueryable();

            // 各メソッドの返り値をモックに差し替える
            mockset.As<IQueryable<BoardEntity>>().Setup( m => m.Provider ).Returns( data.Provider );
            mockset.As<IQueryable<BoardEntity>>().Setup( m => m.Expression ).Returns( data.Expression );
            mockset.As<IQueryable<BoardEntity>>().Setup( m => m.ElementType ).Returns( data.ElementType );
            mockset.As<IQueryable<BoardEntity>>().Setup( m => m.GetEnumerator() ).Returns( data.GetEnumerator() );

            mockcontext.Setup( m => m.Boards ).Returns( mockset.Object );

            var postData = new BoardPostModel { Text = "投稿内容" };

            var controller = new BoardController(mockcontext.Object);
            var result = controller.PostResponse(1, postData ) as RedirectResult;

            //  データの追加がちゃんとされているかチェック
            mockposts.Verify( m => m.Add( It.Is<BoardPostEntity>( o => o.Text == postData.Text ) ), Times.Once );
            mockcontext.Verify( m => m.SaveChanges(), Times.Once );

            Assert.AreEqual( result.Url, "/Board/Show/1" );
        }
 public ActionResult PostResponse( int id, BoardPostModel data )
 {
     var board = (from o in db_.Boards where o.Id == id select o).DefaultIfEmpty( null ).Single();
     if ( board != null ) {
         board.Posts.Add( new BoardPostEntity {
             Text = data.Text
         } );
         db_.SaveChanges();
     }
     return Redirect("/Board/Show/" + id);
 }
 public ActionResult PostResponse( int id, BoardPostModel data )
 {
     db_.PostResponse( id, new BoardPostEntity {
         Text = data.Text
     } );
     return Redirect("/Board/Show/" + id);
 }