2009-11-09 52 views
0

我有一個BLL對用戶輸入進行驗證,然後插入父項(PorEO),然後插入子項(PorBoxEO)。所以有兩個調用相同的InsertJCDC。像這樣=> InsertJCDC(fakePor),另一個像這樣=> InsertJCDC(fakeBox)。structureMap mocks stub help

當我剔除父項時,我想返回fakePor。但是當我運行代碼時,它會返回null。這是單元測試。

[Test] 
     public void PorBLL_InsertByPorInsertCV_DoingGoodCase() 
     { 
      // Startup object mapper 
      _Bootstrapper.Bootstrap(); 

      // create the mock for generic Crud 
      IGenericCrud mockGenericCrud = MockRepository.GenerateMock<IGenericCrud>(); 
      PorInsertCV fakePor = new PorInsertCV(); 
      PorBoxInsertCV fakeBox = new PorBoxInsertCV(); 

      // build fake return 
      PorEO fakePorNewRow = new PorEO(); 
      fakePorNewRow.PorId = 22; 

      // stub parent and child insert routines. 
      mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow); 
      mockGenericCrud.Stub(c => c.InsertJCDC<PorBoxEO, PorBoxInsertCV>(fakeBox)).Return(null); 
      ObjectFactory.Inject(typeof(IGenericCrud), mockGenericCrud); 
      IPorBLL localWithMock = ObjectFactory.GetInstance<IPorBLL>(); 

      // build user args to csll bll with and use for insert 
      PorInsertCV userArgs = new PorInsertCV(); 
      userArgs.AccessionNbr = "364-80-0007"; 
      userArgs.NbrBoxes = 11; 
      userArgs.RegId = 20; 
      userArgs.TransmitedDt = Convert.ToDateTime("1/30/1980"); 

      // call the bll using the stub 
      localWithMock.InsertByPorInsertCV(userArgs); 
     } 

任何幫助是極大的讚賞

+0

你究竟想要測試什麼?我沒有看到任何斷言。 – 2009-11-09 16:45:44

回答

0

我真的不能按照你的代碼,很好,但我給它一個鏡頭。

從我演繹的技巧,在這裏這條線是一個給你的問題:

mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow); 

因爲你期待fakePorNewRow當你調用localWithMock.InsertByPorInsertCV(userArgs);要返回 - 是嗎?

如果這是你的情況,你的問題是,它只會返回fakePorNewRow當它給予fakePor ...不是userArgs,因爲你已經給它。

告訴我,如果我完全偏離軌道。

HTHS,
查爾斯

詩篇。您可能想要將您正在使用哪個模擬框架的標籤添加到問題中。