У меня есть классНедопустимый описатель члена инициализатора в единичном испытании макет
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 Недопустимый декларатор элемента инициализатора.
Вы пропустили CustomerRestrictions = testCustomerRestrictionsList. Вместо этого вы просто передаете testCustomerRestrictionsList в инициализатор объектов. – Evk
Здесь нет места, где вы фактически настраиваете, что 'IBtnRulesRepository' фактически возвращает данные. Нет никакого 'var mock = new Mock(); mock.Setup (x => x.GetRestrictions) ... и т. д. И если вы хотите, чтобы он оставался так, как вы закодировали, то вы делаете интеграционный тест .... –
@CallumLinington, хорошая точка. Но в «Setup/Returns» мне все равно нужно отправить образцы данных. Так что я должен сгенерировать их с помощью жесткого кода? –