单元测试模拟中的初始化成员声明符无效

时间:2016-06-07 13:17:53

标签: c# unit-testing moq

我有一个班级

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无效的初始化成员声明符。

1 个答案:

答案 0 :(得分:0)

我可以看到您的代码有两件事不正确。

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

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

在这种情况下,我不会过分担心将Restrictions硬编码到您的单元测试中。但是在将来你可能想要查看AutoFixture这样的内容,这样你就不必担心创建对象了。

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

您的测试应如下所示:

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