Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 流畅/富有表现力的构建器单元测试和IOC_C#_Unit Testing - Fatal编程技术网

C# 流畅/富有表现力的构建器单元测试和IOC

C# 流畅/富有表现力的构建器单元测试和IOC,c#,unit-testing,C#,Unit Testing,我不得不承认,我对教科书课程的实际应用有点困惑 通过阅读本文和观看MarkSeeman在Pluralsight上的视频,我了解了如何使用builder模式将构造与表示分离 例如: public class Employee { public Employee(int id, string firstname, string lastname, DateTime birthdate, string street) { ....do stuff.. } } 典型的单元测

我不得不承认,我对教科书课程的实际应用有点困惑

通过阅读本文和观看MarkSeeman在Pluralsight上的视频,我了解了如何使用builder模式将构造与表示分离

例如:

public class Employee
{
   public Employee(int id, string firstname, string lastname, DateTime birthdate, string street)
   {
    ....do stuff..
   }
} 
典型的单元测试在“安排”部分会有重复和额外的信息:

使用Fluent/Expressive,我们将有一个构建器来安排测试,因此我们的测试将是

public class EmployeeTest
{

   [Test]
   public void GetFullNameReturnsCombination()
   { 
      // Arrange
      Employee emp = new EmployeeBuilder().WithFirstName("Joe").WithLastName("Blow");
   }
}

这对我的世界有什么影响

在实际应用中,我们使用Ninject for IOC,但我不确定如何将其与构建器模式结合使用

假设我的构造器是这样的:

public Employee(IDepartment _department, IFacility _facility)
在单元测试生成器模式实现中,我的“构建”会像往常一样挂接/注册到Ninject吗

public class EmployeeBuilder
{

  public Employee Build()
  {
    return new Employee(IDepartment _department, IFacility _facility);
  }

}
或者我会在EmployeeBuiler.Build中使用最小起订量吗?毕竟,单元测试的想法是,它能够在单个“单元”上运行,而不必启动整个SUT


所以要明确的问题是,我如何使用IOC的builder模式创建单元测试,或者Moq如何适应……或者Moq与fluent builder模式类似,只是使用Lambda模式?

首先,我认为不应该在单元测试中使用NInject。 在我看来,使用构建器模式的单元测试应该是:(使用伪模拟)


希望我帮了忙

对不起,我很困惑。我不想问一个糟糕的问题。我也不确定我是否理解否决票。这个问题有点啰嗦,但不是不合理的。
public class EmployeeBuilder
{

  public Employee Build()
  {
    return new Employee(IDepartment _department, IFacility _facility);
  }

}
public class EmployeeBuilder
{
    IFacility facility;
    IDepartment department;        

    public Employee Build()
    {
         return new Employee(IDepartment _department, IFacility _facility);
    }

    public EmployeeBuilder WithDepartment(IDepartment _department)
    {
        this.department = department;
        return this;
    }
    public EmployeeBuilder WithFacility(IFacility _facility)
    {
        this.facility = facility;
        return this;
    }

    public static implicit operator Employee(EmployeeBuilder instance)
    {
        return instance.Build();
    }
}
[TestFixture]
public class Tests
{
    [Test]
    public void Test1()
    {
        IFacility facility= MOCK(IFacility);
        IDepartment department= MOCK(IDepartment);
        Employee employee = new EmployeeBuilder().WithFacility(facility).WithDepartment(department);

        [exercise]

        [asserts]
    }
}