We are using SharpRepository on top of EntityFramework 6, that's working great so far. 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. 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.
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:
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.
Research
I've researched this fairly extensively, both Sharp Repository in general and unit testing with it specifically.
I've seen this SO, which led me to use the CacheRepository, How can I seed SharpRepository's 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.