【问题标题】:C# - Unit testing Factory Design PatternC# - 单元测试工厂设计模式
【发布时间】:2018-10-16 16:17:10
【问题描述】:

我正在处理一个项目,我必须在 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; }
    }
}

这是类空间:

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

【问题讨论】:

  • 你到底需要测试什么?
  • 嗨,我想测试 Factory CinemaFactory 是否返回返回新的 Cinema(item.AreaType, item.Position, item.Dimension)。我不知道如何测试它
  • 您有权编辑您的帖子吗?请添加AddItemsInPropsCinemaSpaceAbstractFactory 的定义——基本上是编译代码所需的所有内容
  • 添加了一个测试示例。

标签: c# unit-testing tdd


【解决方案1】:

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

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

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

【讨论】:

  • 我不知道我可以在“var item = new AddItemsInProps”中添加什么,你能帮我解决一下吗
【解决方案2】:

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

一个例子——测试等价性:

[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 来检查对象是否相等。

【讨论】:

  • “cinema.Should().BeEquivalentTo(expectedCinema);”在 C# 中不工作,你有另一行代码如何工作吗?
  • @user9554749 我正在使用 FluentAssertions nuget 包,与 MsTest 或 NUnit 相比,它包含更多更好的断言。我刚刚意识到您使用的是 MSTest(不是 NUnit),我会尽快编辑我的答案
  • 那太好了。谢谢,我将安装 FluentAssertions nuget 包
  • @user9554749 编辑了我的答案,它现在正在使用 MsTest 和 FluentAssertions。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多