gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:40:18 26 4
gpt4 key购买 nike

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

它在我的集成测试中工作,它实际上会访问 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com