FindAsync Test never returns

Mar 18, 2014 at 11:06 PM
Hi, thanks Le Long for the framwork, looks promising. I'm currently trialing it and have struck a problem. I have a test that test FindAsync and never returns? Oddly the actual app works fine.

Here are two tests, first one works, last one doesn't
[TestMethod]
        public void ShouldReturnListOfAllUploadedFiles()
        {
            using (IDataContextAsync asxFakeContext = new AsxFakeContext())
            using (IUnitOfWorkAsync unitOfWorkAsync = new UnitOfWorkAsync(asxFakeContext))
            {
                unitOfWorkAsync.Repository<UploadedFile>().Insert(new UploadedFile { Id = 1, FileName = "fileName1", FilePath = "FilePath1", ObjectState = ObjectState.Unchanged });
                unitOfWorkAsync.Repository<UploadedFile>().Insert(new UploadedFile { Id = 2, FileName = "fileName2", FilePath = "FilePath2", ObjectState = ObjectState.Unchanged });
                unitOfWorkAsync.SaveChanges();

                // Arrange
                var uploadedFileService = new UploadedFileService(unitOfWorkAsync.RepositoryAsync<UploadedFile>());
                var controller = new UploadedFileController(uploadedFileService, unitOfWorkAsync);
                
                // Act
                var result = controller.Index().Result as ViewResult;

                // Assert
                Assert.IsNotNull(result);
                Assert.AreEqual(2, ((IEnumerable<UploadedFile>)result.Model).Count());
            }
        }

        [TestMethod]
        public void ShouldReturnChosenUploadedFile()
        {
            using (IDataContextAsync asxFakeContext = new AsxFakeContext())
            using (IUnitOfWorkAsync unitOfWorkAsync = new UnitOfWorkAsync(asxFakeContext))
            {
                unitOfWorkAsync.Repository<UploadedFile>().Insert(new UploadedFile { Id = 1, FileName = "fileName1", FilePath = "FilePath1", ObjectState = ObjectState.Unchanged });
                unitOfWorkAsync.Repository<UploadedFile>().Insert(new UploadedFile { Id = 2, FileName = "fileName2", FilePath = "FilePath2", ObjectState = ObjectState.Unchanged });
                unitOfWorkAsync.SaveChanges();

                // Arrange
                var uploadedFileService = new UploadedFileService(unitOfWorkAsync.RepositoryAsync<UploadedFile>());
                var controller = new UploadedFileController(uploadedFileService, unitOfWorkAsync);

                // Act
                var result = controller.Details(1).Result as ViewResult;

                // Assert
                Assert.IsNotNull(result);
                Assert.AreEqual("FilePath1", ((UploadedFile)result.Model).FileName);
                Assert.AreEqual("fileName1", ((UploadedFile)result.Model).FilePath);
                Assert.AreEqual(1, ((UploadedFile)result.Model).Id);
            }
        }
The later disappears at this line of the Repository
        public virtual async Task<TEntity> FindAsync(params object[] keyValues)
        {
            return await _dbSet.FindAsync(keyValues);
        }
Coordinator
Mar 19, 2014 at 12:06 AM
The unit test uses mocks, oversimplification is that data is actually stored in memory with Observable for each of the entities, vastly different than storing to an actual database, unit test are typically to test business logic rather than persistence, for these kinds of test setup integration test that creates a database and seeds it with the minimum required test and after this, clean destroy your test database.

Yes, we never quite finished FindAsync for mocks for unit testing due to competing schedules of getting features out for the framework vs. test, will add this to our backlog, thanks.
Marked as answer by lelong37 on 3/31/2014 at 10:24 PM