繁体   English   中英

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

[英]C# - Unit testing Factory Design Pattern

我正在研究一个项目,我必须在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()
    {

    }
}

这是de class 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>();

这是de Abstract AbstractFactory:

    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);
        //...
    }
}

调用测试中的方法,然后使用实际行为验证预期的行为

您可以测试一些内容,例如工厂返回的对象类型以及对象是否等同于预期对象。

一个例子 - 测试等价:

[TestClass]
public class CinemaFactoryTests
{
    [TestMethod]
    public void GivenItemProps_WhenCreatingSpace_ShouldReturnExpectedSpace()
    {
        // arrange
        var factory = new CinemaFactory();

        var props = new AddItemsInProps
        {
            AreaType = "my area",
            Position = "my position",
            Dimension = "my dimension"
        };

        // act
        Space cinema = factory.createspace(props);

        // assert
        var expectedCinema = new Cinema(props.AreaType, props.Position, props.Dimension);
        cinema.Should().BeEquivalentTo(expectedCinema);
    }
}

在这种情况下,我正在检查工厂返回的对象是否等于预期的对象。

PS:我正在使用FluentAssertions来检查对象是否相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM