Python社区  »  MongoDB

如何在c单元测试中模拟MongoDb IMongoCollection?

Andybrad • 2 年前 • 462 次点击  

对这一切都很陌生。我想模拟对mongoDb的调用,以便对我的方法进行单元测试,例如 GetLatestBuildDataRecord()

public class BuildData : BaseData, IBuildData
    {
        private static IMongoCollection<BuildReleaseDefinition> _collection;

        public BuildData(IDatabaseFactory databaseFactory, ProjectConfiguration projectConfiguration)
            : base(projectConfiguration)
        {
            _collection = databaseFactory.GetCollection<BuildReleaseDefinition>(_buildDbName, _buildDbCollectionName) as IMongoCollection<BuildReleaseDefinition>;
        }

        public BuildReleaseDefinition GetLatestBuildDataRecord()
        {
            return _collection.AsQueryable().OrderByDescending(x => x.CreatedOn).FirstOrDefault();
        }

我的想法:我需要模拟从GetCollection返回的IMongoCollection _collection = databaseFactory.GetCollection<BuildReleaseDefinition>(_buildDbName, _buildDbCollectionName) as IMongoCollection<BuildReleaseDefinition>;

但它是一个接口,我希望它返回一个对象集合的具体类作为我的测试数据。所以我将GetCollection改为返回IEnumerable:

public class DatabaseFactory : IDatabaseFactory
    {
        private readonly MongoClient _mongoClient;

        public DatabaseFactory(MongoClient mongoClient)
        {
            _mongoClient = mongoClient;
        }

        public IMongoDatabase GetDatabase(string databaseName)
        {
            return _mongoClient.GetDatabase(databaseName);
        }

        public IEnumerable<T> GetCollection<T>(string databaseName, string collectionName)
        {
            return GetDatabase(databaseName).GetCollection<T>(collectionName) as IEnumerable<T>;
        }
    }

…这样我就可以创造一个 List<BuildReleaseDefinitions>

[Fact]
        public void GetLatestBuildDataRecord_GetRecordSuccess_ReturnsSingleLatestRecord()
        {
            // Arrange
            IEnumerable<BuildReleaseDefinition> definitions = new List<BuildReleaseDefinition>
            {
                new BuildReleaseDefinition { Name = "Build1", BuildDefinitionId = 1, CreatedOn = DateTime.Now.AddHours(-1) },
                new BuildReleaseDefinition { Name = "Build2", BuildDefinitionId = 2, CreatedOn = DateTime.Now }
            };

            var expected = new BuildReleaseDefinition { Name = "Build2", BuildDefinitionId = 2, CreatedOn = DateTime.Now };

            var fakeDatabaseFactory = new Mock<IDatabaseFactory>();

            fakeDatabaseFactory
                .Setup(m => m.GetCollection<BuildReleaseDefinition>(It.IsAny<string>(), It.IsAny<string>()))
                .Returns(definitions);

            var buildData = new BuildData(fakeDatabaseFactory.Object, new ProjectConfiguration { BuildDbName = "Jim", BuildDbCollectionName = "Bob" });

            // Act
            var result = buildData.GetLatestBuildDataRecord();

            // Assert
            result.Should().Equals(expected);
        }

但是当我做测试的时候 _collection=databaseFactory.GetCollection<BuildReleaseDefinition>(_buildDbName,_buildDbCollectionName)作为imongoclection<BuildReleaseDefinition>; 返回空值。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56131
 
462 次点击  
分享到微博