c# - 如何使用 Moq 对带有 stub 的 Windows Azure 表查询进行单元测试?

标签 c# unit-testing moq azure-table-storage

我无法让我的单元测试正常工作。

它在我的集成测试中工作,它实际上会访问 Azure 表存储。

我猜问题是对属性 QueryableEntities 的模拟,它返回 Queryable<Word>来自模拟,但它返回一个 DataServiceQuery 来自 ServiceContext 类。是否可以创建一个类型为 DataServiceQuery 并返回 Queryable 的 stub ?

这是我的代码:

测试

[TestMethod]
    public void GetAExistingWordInStorageShouldReturnCorrectWord()
    {

        Word expected = new Word(Dictionaries.Swedish.ToString(), "Word", "Word");

        List<Word> Words = new List<Word>();
        Words.Add(new Word(Dictionaries.Swedish.ToString(), "Word", "Word"));

        IQueryable<Word> WordQueryable = Words.AsQueryable<Word>();
        
        var mock = new Mock<IServiceContext<Word>>();
        mock.Setup(x => x.QueryableEntities).Returns(WordQueryable);

        DictionaryRepository dr = new DictionaryRepository(Models.Dictionaries.Swedish, "testdictionaries");
        dr.Context = mock.Object;

        Word result = dr.GetWord(expected.Text, false);

        Assert.AreEqual(expected, result);
    }

IServiceContect 接口(interface)

public interface IServiceContext<TEntity>
{
    IQueryable<TEntity> QueryableEntities {get;}
}

ServiceContext 类

public class ServiceContext<TEntity> : TableServiceContext, IServiceContext<TEntity> where TEntity : TableServiceEntity
{

    private readonly string tableName;

    public ServiceContext(CloudStorageAccount account, String tableName)
        : base(account.TableEndpoint.ToString(), account.Credentials)
    {
        this.tableName = tableName;
        this.IgnoreResourceNotFoundException = true;
    }

    public IQueryable<TEntity> QueryableEntities
    {
        get
        {
            return CreateQuery<TEntity>(tableName);
        }
    }

}

词典库

     public class DictionaryRepository : IDictionaryRepository
{
    public Dictionaries Dictionary { get; set; }
    public String TableName;

    public IServiceContext<Word> Context;

    public DictionaryRepository(Dictionaries dictionary)
        : this(dictionary, "dictionaries")
    {
    }

    public DictionaryRepository(Dictionaries dictionary, String tableName)
    {
        Dictionary = dictionary;
        this.TableName = tableName;
        CloudStorageAccount account = CloudStorageAccount.Parse(***);
        Context = new ServiceContext<Word>(account, this.TableName);
    }

    public List<Tile> GetValidTiles()
    {
        throw new NotImplementedException();
    }

    public Type ResolveEntityType(String name)
    {
        return typeof(Word);
    }

    public Word GetWord(string word, Boolean useCache = false)
    {
      
        var q = this.Context.QueryableEntities.Where(x => x.PartitionKey == Dictionary.ToString() && x.RowKey == word).AsTableServiceQuery();

        Word result = q.Execute().SingleOrDefault();

        if (result == null)
            return null;

        return result;

    }} 

我收到以下错误

错误:

    ArgumentNullException was unhandled by user code
    Value cannot be null.
    Parameter name: query

在 DictionaryRepository 类的以下行中调用 .AsTableServiceQuery() 时出现错误:

var q = this.Context.QueryableEntities.Where(x => x.PartitionKey == Dictionary.ToString() && x.RowKey == word).AsTableServiceQuery();

最佳答案

你没有提到你遇到的错误,但是自从 QueryableEntities是只读属性尝试使用 mock.SetupGet而不是 mock.Setup .

编辑:

进一步调查,问题是 .AsTableServiceQuery()扩展方法尝试转换 IQueryable<T>DataServiceQuery<T> ,失败导致空异常。

Frederic Boerr 发表了一篇关于如何使用表存储进行单元测试的帖子,应该可以帮助您解决问题。 Windows Azure Storage: TDD and mocks

关于c# - 如何使用 Moq 对带有 stub 的 Windows Azure 表查询进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9150312/

相关文章:

c# - Youtube API注释顺序不正确/网页无法正确加载youtube

c# - LINQ 需要 40 秒才能结束查询 - 性能

unit-testing - Angular2 : Can't test component with templateUrl using TestBed. compileComponent()

c++ - QT造物主: How to generate the library and testing executable for a custom widget

c# - 将 String 转换为 DateTime 时出错。 "String was not recognized as a valid DateTime."

c# - 如何订购属于一个测试集合但分布在多个测试类中的 xUnit 测试?

c# - 使用 MOQ 模拟系统事件

c# - 管理员返回 null 但为什么?

c# - 在起订量中传递谓词函数参数失败

c# - 属性网格项和 DoubleClick