Git Product home page Git Product logo

Comments (13)

reduckted avatar reduckted commented on August 15, 2024 7

Despite having the provider specified in the app.config file, I was still getting this exception being thrown for my tests that were using Effort. I wasn't using any other provider in that assembly, so I couldn't understand why this exception was still being thrown.

tl;dr version
In one of my other tests I was creating a DbModelBuilder which results in the DbConfiguration being accessed and created. Once it's created, adding a handler to the Loaded event (which Effort does) will throw the exception we are seeing here.

Long Version
One of my tests creates a DbModelBuilder. This has the following call chain:

DbModelBuilder.ctor()
|-- DbModelBuilder.SelectConventionSet()
    |-- V1ConventionSet.cctor()
        |-- NotMappedTypeAttributeConvention.ctor()
            |-- TypeAttributeConfigurationConvention.ctor()
                |-- DbConfiguration.DependencyResolver
                    |-- InternalConfiguration.Instance
                        |-- InternalConfiguration.GetConfiguration() <-- This creates the configuration!!!

Once the configuration has been created, trying to add a handler to the DbConfiguration.Loaded event will throw an exception. Effort tries to add a handler for that event when it is first used.

I managed to work out which test was causing the configuration to be created by adding my own handler to the DbConfiguration.Loaded event and checking the call stack when that event is raised. The call stack should show you what test caused the event to be raised.

Since adding the event handler can throw an exception, I had to add it to the start of every test. I just created a method and called it at the start of every test.

void TrackLoaded() {
    DbConfiguration.Loaded += delegate(object sender, DbConfigurationLoadedEventArgs e) {
            System.Diagnostics.Debugger.Break();
        };
}

Then I just debugged all my tests (Test->Debug->All Tests). Once I worked out what test was causing the Loaded event to fire, I just added Effort.Provider.EffortProviderConfiguration.RegisterProvider() at the start of that test, and now all of the tests run without a problem!

from entityframework-effort.

GPRogers avatar GPRogers commented on August 15, 2024 6

Now that xUnit.NET 2.0 is released, the solution is

  • to implement your own subclass of XunitTestFramework,

  • Do your initialization in the constructor

  • Mark the assembly with a TestFrameworkAttribute.

EG:

using Xunit.Abstractions;
using Xunit.Sdk;

[assembly: Xunit.TestFramework("MyNamespace.MyClassName", "MyAssemblyName")]

namespace MyNamespace
{
    public class MyClassName : XunitTestFramework
    {
        public MyClassName(IMessageSink messageSink) : base(messageSink)
        {
            Effort.Provider.EffortProviderConfiguration.RegisterProvider();
        }
    }
}

from entityframework-effort.

lukaszgulbinski avatar lukaszgulbinski commented on August 15, 2024 2

It happens to me when I use other provider in the same test project and a DbContext with this provider is initialized by test execution process before DbContext with Effort provider.

I have found few workarounds:

  1. Splitting test project into two different assemblies, one for testing with Effort, second for testing with real provider.
  2. Making sure that Effort.Provider.EffortProviderConfiguration.RegisterProvider(); is called before any test method which use DbContext with different provider (ex. [AssemblyInitializeAttribute]).

from entityframework-effort.

vincentw56 avatar vincentw56 commented on August 15, 2024 1

I was having this issue too in NUnit and this is what I did. I created a class file in the test project.

    [SetUpFixture]
    public class TestSetUpClass
    {
        [OneTimeSetUp]
        public void RunBeforeAnyTests()
        {
            Effort.Provider.EffortProviderConfiguration.RegisterProvider();
        }

        [OneTimeTearDown]
        public void RunAfterAnyTests()
        {
        }
    }

The project will see this file and run it before it starts the tests.

from entityframework-effort.

Terebi42 avatar Terebi42 commented on August 15, 2024

I am having the exact same issue. vs2015, mstest

from entityframework-effort.

tamasflamich avatar tamasflamich commented on August 15, 2024

Thank you for reporting this. It seems the problem is that the exception is still thrown when the xml configuration is set. As a workaround there should be an app config that disables this exception.

from entityframework-effort.

darxis avatar darxis commented on August 15, 2024

Any workarounds? Still experiencing this issue...

from entityframework-effort.

millerscout avatar millerscout commented on August 15, 2024

@reduckted you're right, i had a small project and i did mock a context and was doing some tests on that scenario, after you pointed this out, i could fix this problem as well, thanks.

from entityframework-effort.

JamesIlling avatar JamesIlling commented on August 15, 2024

Is there a way to use EFFORT with an existing DataContextConfiguration? If so how.

from entityframework-effort.

Terebi42 avatar Terebi42 commented on August 15, 2024

@JamesIlling This probably isn't the right location for a question like this, this isn't a general purpose forum. But assuming your context class has the overload exposed which accepts a connection, the following will work

var connection = Effort.DbConnectionFactory.CreateTransient();
var context = new MyContext( connection, null );

from entityframework-effort.

PhilHardingRiver avatar PhilHardingRiver commented on August 15, 2024

I have the same issue except it only happens on the build server. The tests run perfectly on my local machine. None of the solutions I have read up on work for me. Any ideas why tests won't run on a second environment?

from entityframework-effort.

dotcom9 avatar dotcom9 commented on August 15, 2024

Now that xUnit.NET 2.0 is released, the solution is

to implement your own subclass of XunitTestFramework,

Do your initialization in the constructor

Mark the assembly with a TestFrameworkAttribute.

EG:
using Xunit.Abstractions;
using Xunit.Sdk;

[assembly: Xunit.TestFramework("MyNamespace.MyClassName", "MyAssemblyName")]

namespace MyNamespace
{
public class MyClassName : XunitTestFramework
{
public MyClassName(IMessageSink messageSink) : base(messageSink)
{
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
}
}
}

Thanks, a solution to this at last! I was intermittently getting the same error when running unit tests in a TeamCity build, though the tests worked fine in Visual Studio.

from entityframework-effort.

StefH avatar StefH commented on August 15, 2024

@GPRogers & @dotcom9 What is the solution for NUnit?

from entityframework-effort.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.