单元测试模型中的初始值设定项成员声明符无效

Invalid initializer member declarator in unit test mock up

我有一个class

public class Restrictions
{
    [Key]
    public short RestrictionId { get; set; }
    public string Description { get; set; }
    public string BlockCode { get; set; }
    public List<CustomerRestrictions> CustomerRestrictions { get; set; }
}

public class CustomerRestrictions
{
    public int CustomerId { get; set; }
    public CustomerContacts CustomerContacts { get; set; }
    public string RestrictionId { get; set; }
    public Restrictions Restrictions { get; set; }
}

然后

public class CustomerContacts
{
    [Key]
    public long CustomerId { get; set; }
    public string Btn { get; set; }
    public List<CustomerRestrictions> CustomerRestrictions { get; set; }
}

好像是多对多关系

现在我想对控制器进行单元测试。有一个类似的example. 但它没有多对多。

我的控制器是

 [Route("api/[controller]")]
public class BtnRulesController : Controller
{
    private readonly IBtnRulesRepository _btnRulesRepository;
    public BtnRulesController(IBtnRulesRepository btnRulesRepository)
    {
        _btnRulesRepository = btnRulesRepository;
    }
    // GET api/BtnRules
    [HttpGet]
    public IList<Restrictions> Get()
    {
        return _btnRulesRepository.GetRestrictions();
    }

此时的难点在于如何在单元测试中创建样本数据。

 public class SimpleBtnRulesControllerTest
{
    [Fact]
    public void GetAllBtnRules_Should_Return_All_BtnRules()
    {
        var repo = new Mock<IBtnRulesRepository>().Object;
        var controller = new BtnRulesController(repo);

        var testBtnRules = GetTestBtnRules();
        var result = controller.Get();
        Assert.Equal(testBtnRules.Count,result.Count);
    }

    public List<Restrictions> GetTestBtnRules()
    {
        var testBtnRules = new List<Restrictions>();

        var testCustomerRestrictionsList = new List<CustomerRestrictions>();
        var testCustomerRestrictions = new CustomerRestrictions();
        testCustomerRestrictions.CustomerId = 1;
        testCustomerRestrictions.RestrictionId = "1";
        testCustomerRestrictions.Restrictions=new Restrictions();
        testCustomerRestrictions.CustomerContacts=new CustomerContacts();
        testCustomerRestrictionsList.Add(new CustomerRestrictions());
        testBtnRules.Add(new Restrictions() {RestrictionId = 1, Description = "Demo1",BlockCode = "AdminBlock1",testCustomerRestrictionsList});
        testBtnRules.Add(new Restrictions() { RestrictionId = 2, Description = "Demo2", BlockCode = "AdminBlock2" ,testCustomerRestrictionsList});
        return testBtnRules;
    }

但是我收到错误 CS0747 初始化程序成员声明符无效。

我看到您的代码有两处不正确。

首先,当您设置 testCustomerRestrictionsList 时,您没有命名要分配给它的 属性,这是编译器错误。 您的代码应如下所示:

testBtnRules.Add(new Restrictions() {RestrictionId = 1, Description = "Demo1",BlockCode = "AdminBlock1", CustomerRestrictions = testCustomerRestrictionsList});

在这种情况下,我不会太担心将 Restrictions 硬编码到您的单元测试中。但将来您可能想研究 AutoFixture 这样的东西,这样您就不必为创建对象担心太多。

其次,当调用 GetRestrictions 方法时,您没有设置对模拟 IBtnRulesRepository 的调用。

您的测试应如下所示:

[Fact]
public void GetAllBtnRules_Should_Return_All_BtnRules()
{
    var repo = new Mock<IBtnRulesRepository>();

    var testBtnRules = GetTestBtnRules();

    repo.Setup(mock => mock.GetRestrictions())
        .Returns(testBtnRules)
        .Verifiable();

    var controller = new BtnRulesController(repo.Object);
    var result = controller.Get();
    Assert.Equal(testBtnRules.Count,result.Count);
    repo.Verify();
}