C# - 单元测试工厂设计模式

标签 c# unit-testing tdd

我正在处理一个项目,我必须在 C# 中对工厂设计模式进行单元测试。 目前我卡住了,我不知道我也做了什么。有人可以帮我吗?我想对这段代码进行单元测试:

public class CinemaFactory : AbstractFactory //inheritance of AbstractFactory
{
    public override Space createspace(AddItemsInProps item)
    {
        //returns new Cinema
        return new Cinema(item.AreaType, item.Position, item.Dimension); 
    }
}

我还制作了一个单元测试项目并创建了这个:

    [TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        //Arrange
        Game1.Design_Patterns.CinemaFactory cinemaFactory = new Game1.Design_Patterns.CinemaFactory();

        //Act

        //Assert

    }
}

电影院:

public class Cinema : Space //inheritance of Space class
{

    //Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. 
    public Cinema(string areaType, string position, string dimension) : base(areaType, position, dimension)
    {
        //The sprite  of the Cinema
        this.Img = "cinema";
    }
    public void StartMovie()
    {

    }
}

这是 AddItemsInProps 类:

public class AddItemsInProps
{
    //Get: The get { } implementation must include a return statement. It can access any member on the class.
    //Set: The set { } implementation receives the implicit argument "value." This is the value to which the property is assigned.

        public string Classification { get; set; }
        public string AreaType { get; set; }
        public string Position { get; set; }
        public string Dimension { get; set; }
        public int Capacity { get; set; }
        public int ID { get; set; }
    }
}

这是 de class Space:

 public abstract class Space
{
    public string AreaType { get; set; }
    public Point Position { get; set; }
    public Point Dimension { get; set; }
    public int ID { get; set; }
    public string Img { get; set; }
    public int Afstand { get; set; }
    public Space Vorige { get; set; }
    public Dictionary<Space, int> Neighbours = new Dictionary<Space, int>();

这是抽象工厂类:

    public abstract class AbstractFactory
{
    //Creating Object
    public abstract Space createspace(AddItemsInProps item);
}

最佳答案

创建被测对象的实例并提供必要的依赖项。

[TestClass]
public class CinemaFactorTests {
    [TestMethod]
    public void CinemaFactory_Should_Create_Cinema() {
        //Arrange
        var cinemaFactory = new Game1.Design_Patterns.CinemaFactory();

        var item = new AddItemsInProps {
            //set necessary properties here
            AreaType = "value here",
            Position = "value here",
            Dimension = "value here"
        };

        //Act
        var actual = cinemaFactory.createspace(item);

        //Assert
        Assert.IsNotNull(actual);
        Assert.IsInstanceOfType(actual, typeof(Cinema));
        //...you can also compare property values from item and actual for equality
        Assert.AreEqual(item.AreaType, actual.AreaType);
        //...
    }
}

调用被测方法,然后用实际行为验证预期行为

关于C# - 单元测试工厂设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839852/

相关文章:

c# - 什么是最好的正则表达式生成器/解释器

c# - 我可以看到 SQL/正在使用 L2S 有效地更新多条记录吗?

unit-testing - 在 firefox-addon-sdk 的单元测试中加载测试页面

c# - MVC + WCF + TDD 或 DDD 架构

c# - 如何在不违反单一职责原则的情况下编写类似的测试用例?

c# - 跨多个属性搜索 List<T> 的最快方法是什么?

c# - 调试 WP7 崩溃

android - 如何测试对 setContentView 的调用?

angularjs - karma 开始 - 传递参数

.net - 我可以在不实际执行的情况下测试 LINQ 查询的构造吗