N Th
N Th

Reputation: 35

I have problem in writing test for one of my class that contain DBset

I wrote this part of code and I want write a test for it but I can't, because of the DBSet. this part of code tries to fill a table in the database and in my test I wanna check if it is initialized correctly.

public void BuildInvertedIndex(Dictionary<string, string> documents)
{
    foreach (var doc in documents)
    {
        var words = Regex.Split(doc.Value, "[\\W]+");
        var docu = new Document(doc.Key, doc.Value);
        foreach (var wordIterator in words)
        {
            var word = _invertedIndexContext.WordsDbContext.Find(wordIterator);
            
            if (word == null)
            {
                _invertedIndexContext.WordsDbContext.Add(new Word(wordIterator, new HashSet<Document>() {docu}));
                _invertedIndexContext.SaveChanges();
            }
            else
            {
                word.DocsCollection.Add(docu);
            }
        }
    }
    
}

and I wrote this test for it but it doesn't work: and in this test, we are checking that each word relates truly to documents that contain it.

private readonly Dictionary<string,string> _docNameMapToContent;
private InvertedIndexContext _context;


public InvertedIndexTest()
{
    var options = new DbContextOptionsBuilder<InvertedIndexContext>()
         .UseInMemoryDatabase( "EfcorePhase08Project")
         .Options;
    _context = new InvertedIndexContext(options);
    _docNameMapToContent = new Dictionary<string, string>
    {
        {"text1", "one two"}, {"text2", "five six seven eight nine"}, {"text3", "one two three "}
    };

}

[Fact]
public void TestBuildInvertedIndex_WordExistInADoc() {
    var invertedIndex = new InvertedIndex(_context);
    invertedIndex.BuildInvertedIndex(_docNameMapToContent);
    
    Assert.Equal(new HashSet<string>(){"text2"}, 
        _context.WordsDbContext.Find("six")?.DocsCollection.Select(w => w.DocName));
}

and it showed for me this error :

System.InvalidOperationException: Services for database providers 'Microsoft.EntityFrameworkCore.InMemory', 'Microsoft.EntityFrameworkCore.SqlServer' ...

System.InvalidOperationException Services for database providers 'Microsoft.EntityFrameworkCore.InMemory', 'Microsoft.EntityFrameworkCore.SqlServer' have been registered in the service provider. Only a single database provider can be registered in a service provider. If possible, ensure that Entity Framework is managing its service provider by removing the call to 'UseInternalServiceProvider'. Otherwise, consider conditionally registering the database provider, or maintaining one service provider per database provider.

Upvotes: 0

Views: 146

Answers (1)

N Th
N Th

Reputation: 35

I can found the answer so I had a class that its name is InvertedIndexContext so I had a OnConfiguring function in it, so I created a new static class like this and moved OnConfiguring function to this new class:

public class InvertedIndexFactory : IDesignTimeDbContextFactory<InvertedIndexContext>
{
    public InvertedIndexContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<InvertedIndexContext>();
        builder.UseSqlServer("Server=. ; Database=EfcorePhase08Project ; Trusted_Connection=true; ");
        return new InvertedIndexContext(builder.Options);
    }
}

Upvotes: 0

Related Questions