I have a business logic code that I want to verify.
At the moment, I only know how to write unit test by logical code, which has no other dependencies.
Can someone point me in the right direction, how to check, for example, this function and maybe give an example?
Is the only way to test this integration test or do I need to use mock / stub?
/// <summary> /// Gets the scan face file by a MemberID /// </summary> /// <param name="MemberID">The ID of a member</param> /// <returns>A scan face file in byte array format</returns> public byte[] GetScanFileFaceByMemberID(int MemberID) { byte[] scanFileFace; using (ProductionEntities entityContext = new ProductionEntities()) { scanFileFace = (from scan in entityContext.tblScan where scan.MEMBERID == MemberID select scan.scanFileFace).Single(); } return scanFileFace; }
CHANGES (I implemented the repository and rhinos):
BL:
public byte[] GetScanFileFaceByMemberID(int MemberID) { byte[] scanFileFace; var query = Repository.GetAll<tblScan>().Where(bl => bl.MEMBERID == MemberID).Single(); scanFileFace = query.scanFileFace; return scanFileFace; }
Unit test:
[TestMethod] public void GetScanFileFace_ExistingScan_ReturnByteArray() { //Make testScan List<tblScan> testScan = PrepareTestDataScan(); //Arrange KlantenBL klantenBL = new KlantenBL(); klantenBL.Repository = MockRepository.GenerateMock<IRepository>(); klantenBL.Repository.Stub(bl => bl.GetAll<tblScan>()).IgnoreArguments().Return(testScan); //Act var result = klantenBL.GetScanFileFaceByMemberID(2); //assert Assert.AreEqual(result.GetType().Name, "Byte[]"); Assert.AreEqual(result.Length, 10); } //Prepare some testData private List<tblScan> PrepareTestDataScan() { List<tblScan> scans = new List<tblScan>(); //Declare some variables byte[] byteFile = new byte[4]; byte[] byteFile10 = new byte[10]; DateTime date = new DateTime(2012,01,01); scans.Add(new tblScan { SCANID = 1, MEMBERID = 1, scanFileFace = byteFile, Hair = byteFile, scanFileAvatar = byteFile, scanFileMeasurements = byteFile, scanDate = date }); scans.Add(new tblScan { SCANID = 2, MEMBERID = 2, scanFileFace = byteFile10, Hair = byteFile, scanFileAvatar = byteFile, scanFileMeasurements = byteFile, scanDate = date }); scans.Add(new tblScan { SCANID = 3, MEMBERID = 3, scanFileFace = byteFile, Hair = byteFile, scanFileAvatar = byteFile, scanFileMeasurements = byteFile, scanDate = date }); return scans; }
Repository:
public IList<T> GetAll<T>() { DZine_IStyling_ProductionEntities context = GetObjectContext(); IList<T> list = context .CreateQuery<T>( "[" + typeof(T).Name + "]") .ToList(); ReleaseObjectContextIfNotReused(); return list; } public IList<T> GetAll<T>(Func<T, bool> expression) { DZine_IStyling_ProductionEntities context = GetObjectContext(); IList<T> list = context .CreateQuery<T>( "[" + typeof(T).Name + "]") .Where(expression) .ToList(); ReleaseObjectContextIfNotReused(); return list; }
It worked great thanks to everyone!