2015-09-21 73 views
0

我想問下面的方法什麼是一個好的單元測試GetMeetingsByInterimIdentifier其中臨時標識符是一個字符串 - 比如78th2015。如何進行單元測試而不碰到數據庫

我們設置爲使用接口IMeetingsService。我們正在使用MOQ和Microsoft.VisualStudio.TestTools.UnitTesting。

public class MeetingsService : IMeetingsService 
{ 
    private readonly IInterimCommitteeDbContext _db; 

    public MeetingsService(IInterimCommitteeDbContext db) 
    { 
     this._db = db; 
    } 

    public IQueryable<Meeting> GetMeetingsByInterimIdentifier(string interimIdentifier) 
    { 
     return 
      from m in this._db.Meetings 
      join c in this._db.Committees on m.CommitteeId equals c.CommitteeId 
      where c.InterimIdentifier == interimIdentifier 
      select m; 
    } 

    public Meeting GetMeeting(int meetingKey) 
    { 
     return this._db.Meetings.FirstOrDefault(x => x.MeetingId == meetingKey); 

    } 

} 

編輯:

但我不知道如何設置它。這個結果不是null,但它對我有什麼作用?

[TestMethod] 
public void GetMeetingsByInterimIdentifier_WithInterimIdentifier_ReturnsMeetingList() 
{ 
    //Arrange 
    var interim = Properties.Settings.Default.DefaultInterimIdentifier; 
    var result = _meetingServiceMock.Setup(x => x.GetMeetingsByInterimIdentifier(interim)); 

    //Act 

    //Assert 
    Assert.IsNotNull(result); 
} 
+0

我可能不會打擾單元測試此。您正在測試基於過濾器('interimIdentifier')從EF返回數據庫對象。在執行LINQ語句後,您的代碼不會處理結果,因此您最好創建實際觸及數據庫的集成測試。 – levelnis

回答

0

使用Mocking,就是這樣。使用JMockMockito或您喜歡的任何其他庫。

+0

這不是一個答案,更重要的是;你提供的OP是一個不相關的框架,因爲他使用'C#'而不是'Java',並且在單行中不是一個答案... –

0

創建一個Mock<IInterimCommitteeDbContext>並將其傳遞給構造函數。在此對象上設置MeetingsCommittees屬性以返回各種集合。

你應該有不同的測試設置,返回不同的集合。例如,如果MeetingsCommittees都爲空,即數據庫中沒有數據,這應該如何表現?如果沒有提供InterimIdentifier的對象,它應該如何處理?怎麼樣,如果有一個匹配等。

+0

但我不確定如何設置它。這不起作用: – kfwbird

+0

也許你可以添加你到目前爲止的問題,或者用你已有的東西創建一個新的問題? – TomDoesCode

+0

但我不知道如何設置它。這個結果不是null,但它對我有什麼作用? [TestMethod的] 公共無效GetMeetingsByInterimIdentifier_WithInterimIdentifier_ReturnsMeetingList() { //排列 變種臨時= Properties.Settings.Default.DefaultInterimIdentifier; var result = _meetingServiceMock.Setup(x => x。GetMeetingsByInterimIdentifier(臨時)); // Act //斷言 Assert.IsNotNull(result); } – kfwbird

0

我想通過測試雙打如何做到這一點。我使用實體框架6和代碼優先模型。我創建了一個從我的I-DbContext接口繼承的DbContext。然後我能夠創建內存數據以用於我的服務層單元測試。下面是一個例子:

  1. 測試數據上下文,
  2. 測試dbset,
  3. 一個例子的單元測試。

該解決方案提供了從MSDN文章在這裏: https://msdn.microsoft.com/en-us/data/dn314429.aspx ...

 
     public class CommitteeContextTest : ICommitteeDbContext 
     { 
      public CommitteeContextTest() 
      { 
       this.Committees = new TestDbSet(); 
       this.CommitteeMembers = new TestDbSet(); 
      } 
      public Database Database { get; } 
      public DbSet Committees { get; set; } 
      public DbSet CommitteeMembers { get; set; } 
     } 
    } 

 
public class TestDbSet : DbSet, IQueryable, IEnumerable, IDbAsyncEnumerable 
      where TEntity : class 
    { 
     ObservableCollection _data; 
     IQueryable _query; 

     public TestDbSet() 
     { 
      _data = new ObservableCollection(); 
      _query = _data.AsQueryable(); 
     } 

     public override TEntity Add(TEntity item) 
     { 
      _data.Add(item); 
      return item; 
     } 

     public override TEntity Remove(TEntity item) 
     { 
      _data.Remove(item); 
      return item; 
     } 

     public override TEntity Attach(TEntity item) 
     { 
      _data.Add(item); 
      return item; 
     } 

     public override TEntity Create() 
     { 
      return Activator.CreateInstance(); 
     } 
    } 

    [TestClass] 
    public class CommitteeServiceTest 
    { 
     private InterimCommitteeContextTest _interimCommitteeContext; 
     private ICommitteeService _service; 
     private string _interim; 

     [TestInitialize] 
     public void SetUp() 
     { 
      _interimCommitteeContext = new InterimCommitteeContextTest(); 
      _service = new CommitteeService(_interimCommitteeContext); 
      _interim = Settings.Default.DefaultInterimIdentifier; 
     } 

     [TestCleanup] 
     public void Teardown() 
     { 
      _interimCommitteeContext = null; 
      _service = null; 
     } 

     [TestMethod] 
     public void GetCommittee_ProvideInterimCommitteeId_ReturnOneCommittee() 
     { 
      //Arrange 
      AddCommittees(); 

      //Act and Assert 
      var result = _service.GetCommittee(_interim, 1); 
      Assert.AreEqual(1, result.CommitteeId); //Passes. IsActive set to true; 
      result = _service.GetCommittee(_interim, 0); 
      Assert.IsNull(result); //Fails. No committeeId = 0; 
      result = _service.GetCommittee(_interim, 2); 
      Assert.IsNull(result); //Fails. CommitteeId = 2 is not active. 
     } 

     [TestMethod] 
     public void AddCommittees() 
     { 
      _interimCommitteeContext.Committees.Add(new Committee() { CommitteeId = 1, InterimIdentifier = _interim, IsActive = true, CommitteeTypeId = 1 }); 
      _interimCommitteeContext.Committees.Add(new Committee() { CommitteeId = 2, InterimIdentifier = _interim, IsActive = false, CommitteeTypeId = 1 }); 
      _interimCommitteeContext.Committees.Add(new Committee() { CommitteeId = 3, InterimIdentifier = _interim, IsActive = true, CommitteeTypeId = 1 }); 
     } 
    }