English 中文(简体)
与Ninpit 结合测试
原标题:Integration test with Ninject

我很熟悉这个事实, 我不该在单位测试中使用依赖注射, 这样我就可以单独测试每一层。

不过,我想为我的控制器建立整合测试。 因此,我需要从一个单位测试中将我的储存库输入到控制器中。

我采用一种通用方法,使用T4脚本,为每个控制器设置一个测试类,其中含有每个动作Result的测试方法。这个测试方法应简单调用该方法,以确保没有例外情况被抛到表面。

由于使用此 T4 脚本, 我无法手动将存储器输入控制器。 我需要使用依赖注射 。

根据我以前的经验,这应该有用, 但我一直得到错误:

Unable to get default constructor for class <<UnitTest>>

我创造的班级现在看起来是这样的:

[TestClass]
public class TestControllersHomeController
{
    private EL.NET.Web.Controllers.HomeController c;
    //setup
    public TestControllersHomeController(Project.Controllers.HomeController c)
    {
        this.c = c;
    }
    [ClassInitialize]
    public void ClassInitialize()
    {

        var kernel = NinjectWebCommon.CreatePublicKernel();
        kernel.Bind<TestControllersHomeController>().ToSelf();
        kernel.Bind<Project.Controllers.HomeController>().ToSelf();
    }
    [TestMethod]
    public void TestIndex()
    {
        var result = c.Index();
        Assert.IsNotNull(result);
    }

编辑 :

I already found out, that the repositories can be loaded with help of the GetService() method of IKernel. But for the Membership provider, this doesn t work. Again, I do not want to mock the provider, I want to perform an integration test so I know if my Controller methods throw any exceptions.

最佳回答

单位测试必须有一个默认构建器 :

[TestClass]
public class TestControllersHomeController
{
    private HomeController _sut;

    [TestInitialize]
    public void MyTestInitialize() 
    {
        var kernel = NinjectWebCommon.CreatePublicKernel();
        _sut = kernel.Resolve<HomeController>();
    }

    [TestMethod]
    public void TestIndex()
    {
        var result = _sut.Index();
        Assert.IsNotNull(result);
    }
}
问题回答

暂无回答




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签