MVC中的单元测试项目 [英] Unit Test Project in MVC

查看:140
本文介绍了MVC中的单元测试项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个项目使用Windsor Castle实现依赖注入。

我必须编写相同的单元测试项目。

我是新手编写单元测试项目任何人都可以在下面的场景中提供帮助。

我能够实现传统的单元测试方法,但我无法写在这个项目中实现的依赖注入的测试方法。

如何为使用依赖注入的类中编写的方法编写测试方法(Windsor Castle用于依赖注入)



任何指针都会很明显。

谢谢。

Hello,

I have a project implementing Dependency injection using Windsor Castle.
I have to write Unit Test Project for the same.
I'm new to writing Unit test Project Could anyone help in below scenario.
I was able to implement the conventional unit test methods but I'm not able to write test methods for the Dependency Injection implemented in this project.
How can I write test methods for the methods written in a class which have dependency injection used (Windsor Castle is used for Dependency Injection)

Any pointers will be appreciable.
Thanks.

推荐答案

有一个已知的概念单元测试时的'Setup'和'Teardown'。



您可以在测试类中定义对测试框架有特殊意义的方法(即MS Test, NUnit)正在执行你的测试。您可以在MS Test类中使用以下属性,这些属性指定要运行的代码



- 在每次测试运行之前 [TestInitialize]

- 测试完成后运行TestCleanup]

- 在任何测试运行之前[ClassInitialize]

- 执行完所有测试后[ClassCleanup]



我自己不使用CastleWindsor,但是你的代码看起来就像下面一行......



There is a concept known as 'Setup' and 'Teardown' when unit testing.

You can define methods in your test class which have special significance to the test framework (ie. MS Test, NUnit) which is executing your tests. You can use following attributes with MS Test class, which specify code to be run

- before each test is run [TestInitialize]
- after a test has finished running TestCleanup]
- before any tests are run [ClassInitialize]
- after all tests have been executed [ClassCleanup]

I don't use CastleWindsor myself, but your code would look something along following lines ...

namespace MyTestProject
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class MyTestClass
    {
        private static IWindsorContainer _Container;

        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            // create dependency container, and perform registrations for SUT
            var container = new WindsorContainer();
            container.Register(
                Component.For<IObjectToTest>().ImplementedBy<ObjectToTest>()
                );
            _Container = container;
        }

        [TestMethod()]
        public void MyTestMethod()
        {
            IObjectToTest obj = _Container.Resolve<IObjectToTest>(); // arrange
            var result = obj.MyMethod(); // act
            Assert.IsNotNull(obj); // assert
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            _Container.Dispose(); // dispose of container when all tests are complete
        }
    }
}





希望有助于您入门!



Hope that helps get you started!


这篇关于MVC中的单元测试项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆