简体   繁体   English

SharpRepository单元测试CacheRepository无法添加多个项目

[英]SharpRepository Unit Testing CacheRepository Unable to Add Multiple Items

We are using SharpRepository on top of EntityFramework 6, that's working great so far. 我们在EntityFramework 6的顶部使用SharpRepository,到目前为止效果很好。 We are also implementing full unit testing, in researching the approved method to unit test with SharpRepository is with the CacheRepository, this lets us populate a repo with consistent test data and then tests services and models against those test repositories. 我们还正在实施完整的单元测试,在研究批准的方法中,通过SharpRepository与SharpRepository进行单元测试是可以的,这使我们可以用一致的测试数据填充存储库,然后针对这些测试存储库测试服务和模型。 This is also working for the most part. 这在大多数情况下也起作用。

The Issue 问题

We have populated these repos successfully, with a single object, then we can pull that object out and test against it. 我们已经成功地用单个对象填充了这些存储库,然后我们可以将该对象拉出并对其进行测试。 but a new test use case, required us to test against multiple entries, when we add them, only the last entry is actually available in the Repo. 但是一个新的测试用例要求我们针对多个条目进行测试,当我们添加它们时,回购中实际上只有最后一个条目可用。 I see this both from the unit test output, and inspecting the repo in the watch window while debugging, it only ever has one. 我既可以从单元测试输出中看到这一点,也可以在调试时在监视窗口中检查回购,这只有一次。

I have setup a standard cache strategy and an inmemory provider, i have also tried adding each object individually and as a list, with the Add function. 我已经设置了一个标准的缓存策略和一个内存提供程序,我还尝试了使用Add函数将每个对象单独添加并作为列表添加。

The Code 编码

the Setup 设置

  [TestInitialize] public void Initialize() { var sharpRepositoryConfiguration = new SharpRepositoryConfiguration(); sharpRepositoryConfiguration.AddCachingStrategy(new StandardCachingStrategyConfiguration("standard")); sharpRepositoryConfiguration.AddCachingProvider(new InMemoryCachingProviderConfiguration("inmemory")); sharpRepositoryConfiguration.AddRepository(new CacheRepositoryConfiguration("textFilter", "TextFilter", "standard", "inmemory")); textFilterRepo = sharpRepositoryConfiguration.GetInstance<TextFilter, int>("textFilter"); } [TestCleanup] public void CleanUp() { textFilterRepo.Delete(c => true); } 

these methods should only be called before and after each test method respectively, so it should not cause issue inside the method itself. 这些方法仅应分别在每个测试方法之前和之后调用,因此不应在方法本身内部引起问题。

The Add 添加

  List<TextFilter> badwords; var badword1= new TextFilter { ID = 0, FilterType = 1, FilterText = "badword1", DateTimeCreated = DateTime.Now }; var badword2 = new TextFilter { ID = 1, FilterType = 1, FilterText = "badword2", DateTimeCreated = DateTime.Now }; var badword3 = new TextFilter { ID = 2, FilterType = 1, FilterText = "badword3", DateTimeCreated = DateTime.Now }; badwords = new List<TextFilter> { badword1, badword2, badword3 }; textFilterRepo.Add(badwords); 

As i said before i've tried both individual add calls and now the list, same results. 就像我之前说过的那样,我尝试过两个单独的添加调用以及现在的列表,结果都是一样的。

EDIT: 编辑: SharpRepositorySettings I'm not sure why CacheUsed is set to false, i do not have additional settings for these, but i'm wondering if this is part of the problem and if I need to call something explicitly to get this to work. 我不确定为什么CacheUsed设置为false,我没有其他设置,但是我想知道这是否是问题的一部分,是否需要显式调用才能使它起作用。

Research 研究

I've researched this fairly extensively, both Sharp Repository in general and unit testing with it specifically. 我已经对它进行了相当广泛的研究,无论是在一般的Sharp Repository还是专门针对它的单元测试中。

I've seen this SO, which led me to use the CacheRepository, How can I seed SharpRepository's InMemoryRepository? 我已经看到了这种方式,这导致我使用了CacheRepository, 如何为SharpRepository的InMemoryRepository设置种子? . But I have not been able to find any info on this information. 但是我无法找到有关此信息的任何信息。 It could be that i have setup the caching for the repository incorrectly, but this is what it typically looks like in the examples i've seen. 可能是我为存储库设置了错误的缓存,但这就是我所看到的示例中通常的样子。 I have used this successfully in many unit tests, but have not had to populate multiple values before. 我已经在许多单元测试中成功使用了此方法,但之前不必填充多个值。

Request 请求

I'm looking for both help in identifying why this setup cannot add multiple objects to it's cache, and how i can change this code, or my unit test setup in general to be able to accommodate multiple objects in my CacheRepository. 我正在寻找有助于确定此设置为何无法向其缓存中添加多个对象的帮助,以及我如何更改此代码,或者总体而言我的单元测试设置以能够在CacheRepository中容纳多个对象。

So i finally hopped into the Sharp Repo source code, and found my issues. 因此,我终于跳入Sharp Repo源代码,并发现了我的问题。 First i will apologize, much of this could have been avoided, i sanitized my code example and naming conventions to make it easier to read, which covered up that my key value had a naming convention mismatch with Sharp Repo. 首先,我会道歉,很多事情本可以避免的,我对代码示例和命名约定进行了清理以使其易于阅读,这掩盖了我的关键价值与Sharp Repo的命名约定不匹配。 the issue is that sharp uses a convention based mapping for ID (which is actually a huge convenience). 问题在于,sharp对ID使用基于约定的映射(这实际上是极大的便利)。

在此处输入图片说明在此处输入图片说明

for those interested i found this by tracing through the AddItem method in the CacheRepositoryBase.cs. 对于那些感兴趣的人,我通过跟踪CacheRepositoryBase.cs中的AddItem方法发现了这一点。 The convention specific code is in DefaultRepositoryConventions.cs you'd need to pull the source to see that obviously but for future knowledge seekers, if you're having issues that might be worth a check. 约定特定的代码位于DefaultRepositoryConventions.cs中,如果您遇到可能值得检查的问题,则需要拉出源才能清楚地看到,但对于将来的知识寻求者而言。

It's worth mentioning that i also got the [RepositoryPrimaryKey] Attribute to work, as well as renaming my property to match the convention, both worked equally well. 值得一提的是,我还获得了[RepositoryPrimaryKey]属性,并且重命名了我的属性以符合约定,两者都同样有效。 The annotation will be particularly helpful with auto-generated EF entities by using a MetaData partial class. 通过使用MetaData子类,注释对于自动生成的EF实体特别有用。

Many thanks to @JeffTreuting, for is help. 非常感谢@JeffTreuting,为您提供了帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM