KVN
KVN

Reputation: 982

Failed to create fake when default constructor for the object is not empty

I'm new to FakeItEasy, and I have a few test cases which are used to pass before (when I had no specific constructors defined). Then I have created a default constructor for one for the object I'm faking, the default constructor only create logger.

After I have created this constructor all my test cases are failing now, with following error, and I cannot figure out what it does not like or how to fix it:

FakeItEasy.Core.FakeCreationException : 
  Failed to create fake of type StorageService.
  Below is a list of reasons for failure per attempted constructor:
    No constructor arguments failed:
      No usable default constructor was found on the type StorageService.
      An exception of type System.NullReferenceException was caught during this call. Its message was:
      Object reference not set to an instance of an object.

Here is my class:

public class StorageService : IStorageService
{
    public StorageService()
    {
        Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().Enrich.FromLogContext().WriteTo.Loggly(
            logglyConfig: new LogglyConfiguration
            {
                //Log Config is here
            }).CreateLogger();
    }

    public async Task<bool> CreateTableIfNotExistsAsync(string storageConnectionString, string tableName)
    {
        // Some code here

        return result;
    }
}

Here is my Test Case:

[Test]
public void create_storage()
{
    FakedStorageService = A.Fake<StorageService>(); // It is failing at this line
    // Some FakeCalls here
}

Upvotes: 0

Views: 2263

Answers (1)

ProgrammingLlama
ProgrammingLlama

Reputation: 38767

I think the issue is down to you trying to create a fake of the implementation instead of the interface. You can modify it to this:

FakedStorageService = A.Fake<IStorageService>();

Upvotes: 4

Related Questions