單元測試和集成測試業務應用程序(4)

發表于:2016-06-01來源:不詳作者:Omar Al Zabir點擊數: 標簽:單元測試
}); }); }); } }); } 需要進一步的解釋:為從模板用戶發現每個頁面確保新用戶從模板用戶的頁面的部件獲得完全一樣的頁面。獲得來自新用戶的頁面的窗口小

  });

  });

  });

  }

  });

  }

  需要進一步的解釋:為從模板用戶發現每個頁面確保新用戶從模板用戶的頁面的部件獲得完全一樣的頁面。獲得來自新用戶的頁面的窗口小部件比較每個插件。當在做業務層的變化對于每個插件確保具有相同的名稱,狀態,位置等獨一無二的部件,我可以運行集成測試,以確保關鍵功能是否按預期工作完成,而且在整個業務層沒有破損任何地方。 我用xunit.console.exe上運行的集成測試測試并生成一個不錯html報告:

  該報告使用下面的命令行產生:

  d:\xunit\xunit.console.exe

  d:\trunk\src\Dropthings.Business.Facade.Tests\bin\Debug\Dropthings.Business.Facade.Tests.dll

  /html FacadeTest.html

  您可以使用GUI xUnit:

  使用BDD的單元測試測試驅動開發

  到目前為止,我們已經通過代碼編寫測試,但如果你先代碼編寫測試有關驅動開發?假設我們要添加行為:給定一個PageRepository ,當 Insert被調用時,它應該在數據庫中插入頁面,清除了得到的新頁面,用戶頁面任何緩存集合,返回新插入的頁面。

  編寫測試代碼:

  [Specification]

  public void InsertPage_should_insert_a_page_in_database_and_cache_it()

  {

  var cache = new Mock();

  var database = new Mock();

  IPageRepository pageRepository = new PageRepository(database.Object, cache.Object);

  const int pageId = 1;

  var page = default(Page);

  var samplePage = new Page() { ID = pageId, Title = "Test Page", ColumnCount = 3,

  LayoutType = 3, UserId = Guid.NewGuid(), VersionNo = 1,

  PageType = Enumerations.PageTypeEnum.PersonalPage, CreatedDate = DateTime.Now };

  database

  .Expect(d => d.Insert(DropthingsDataContext.SubsystemEnum.Page,

  It.IsAny>()))

  .Returns(samplePage);

  "Given PageRepository".Context(() =>

  {

  // It will clear items from cache

  cache.Expect(c => c.Remove(CacheSetup.CacheKeys.PagesOfUser(samplePage.UserId)));

  });

  "when Insert is called".Do(() =>

  page = pageRepository.Insert((newPage) =>

  {

  newPage.Title = samplePage.Title;

  newPage.ColumnCount = samplePage.ColumnCount;

  newPage.LayoutType = samplePage.LayoutType;

  newPage.UserId = samplePage.UserId;

  newPage.VersionNo = samplePage.VersionNo;

  newPage.PageType = samplePage.PageType;

  }));

  ("then it should insert the page in database" +

  "and clear any cached collection of pages for the user who gets the new page" +

  "and it returns the newly inserted page").Assert(() =>

  {

  database.VerifyAll();

  cache.VerifyAll();

  Assert.Equal(pageId, page.ID);

  });

  }

  首先,我們將寫一些虛擬代碼PageRepository.Insert方法,返回一個新的Page。它應該會fail,因為它不滿足目前數據庫對象的期望集。如果沒有失敗,則表明我們的測試是錯誤的。

  public Page Insert(Action populate)

  {

  return new Page();

  }

  運行故障測試結果如預期:

  TestCase 'Given PageRepository when InsertPage is called, then it should insert the

  page in databaseand clear any cached collection of pages for the user who gets the

  new pageand it returns the newly inserted page'

  failed: Moq.MockVerificationException : The following expectations were not met:

  IDropthingsDataContext d => d.Insert(Page, null)

  at Moq.Mock`1.VerifyAll()

  PageRepositoryTest.cs(278,0): at

  Dropthings.DataAccess.UnitTest.PageRepositoryTest.<>c__DisplayClass35.

  b__34()

  這表明,沒有呼叫database.Insert ,所以測試失敗。我們實現了TDD的第一步,這是寫一個測試并使其失敗以來的第一期望沒有正確組件下檢驗。

原文轉自:http://www.codeproject.com/Articles/44276/Unit-Testing-and-Integration-Testing-in-Business-A

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97