C# 传递给单元测试的Ninject DI参数
这是我的构造函数,具有ILogger依赖性C# 传递给单元测试的Ninject DI参数,c#,.net,ninject,C#,.net,Ninject,这是我的构造函数,具有ILogger依赖性 public Book(string isbn, string author, string title, string publisher, int publishYear, ushort pageNumber, decimal price, ILogger logger) { Isbn = isbn; Author = author; Title = title; Publi
public Book(string isbn, string author, string title, string publisher, int publishYear, ushort pageNumber, decimal price, ILogger logger)
{
Isbn = isbn;
Author = author;
Title = title;
Publisher = publisher;
PublishYear = publishYear;
PageNumber = pageNumber;
Price = price;
_logger = logger;
_logger.Log(LogLevel.Info, "Book constructor has been invoked");
}
我的单元测试尝试通过Ninject框架解决它
[TestCase("9783161484100", "Some Name", "C# in a nutshell", "Orelly", 2014, (ushort)900, 60, ExpectedResult = "Some Name Orelly")]
[Test]
public string FormatBook_FortmattingBooksObject_IsCorrectString(string isbn, string author, string title, string publisher, int year, ushort pages, decimal price)
{
using (IKernel kernel = new StandardKernel())
{
var book = kernel.Get<Book>();
Console.WriteLine(book.FormatBook(book.Author, book.Publisher));
return book.FormatBook(book.Author, book.Publisher);
}
}
[TestCase(“9783161484100”、“某个名字”、“简而言之”、“奥雷利”,2014,(ushort)900,60,ExpectedResult=“某个名字奥雷利”)]
[测试]
公共字符串格式书\u forMattingBooks对象\u是正确的字符串(字符串isbn、字符串作者、字符串标题、字符串发布者、整数年、ushort页面、十进制价格)
{
使用(IKernel kernel=new-StandardKernel())
{
var book=kernel.Get();
Console.WriteLine(book.FormatBook(book.Author,book.Publisher));
返回book.FormatBook(book.Author、book.Publisher);
}
}
问题是,如何注入依赖项并将参数传递给构造函数?没有必要在单元测试中注入内容。只需为记录器使用模拟对象:
[TestCase("9783161484100", "Some Name", "C# in a nutshell", "Orelly", 2014, (ushort)900, 60, ExpectedResult = "Some Name Orelly")]
[Test]
public string FormatBook_FortmattingBooksObject_IsCorrectString(string isbn, string author, string title, string publisher, int year, ushort pages, decimal price)
{
ILogger fakeLogger = ...; //Create some mock logger for consumption
var book = new Book(isbn, author, title, publisher, year, pages, price, fakeLogger);
Console.WriteLine(book.FormatBook(book.Author, book.Publisher));
return book.FormatBook(book.Author, book.Publisher);
}
这并不是说,如果您选择这样做,那么记录器也不能由内核提供,但您必须先设置它,然后再获取它:
[TestCase("9783161484100", "Some Name", "C# in a nutshell", "Orelly", 2014, (ushort)900, 60, ExpectedResult = "Some Name Orelly")]
[Test]
public string FormatBook_FortmattingBooksObject_IsCorrectString(string isbn, string author, string title, string publisher, int year, ushort pages, decimal price)
{
INinjectModule module = ...;//Create a module and add the ILogger here
using (IKernel kernel = new StandardKernel(module))
{
var fakeLogger = kernel.Get<ILogger>(); //Get the logger
var book = new Book(isbn, author, title, publisher, year, pages, price, fakeLogger);
Console.WriteLine(book.FormatBook(book.Author, book.Publisher));
return book.FormatBook(book.Author, book.Publisher);
}
}
[TestCase(“9783161484100”、“某个名字”、“简而言之”、“奥雷利”,2014,(ushort)900,60,ExpectedResult=“某个名字奥雷利”)]
[测试]
公共字符串格式书\u forMattingBooks对象\u是正确的字符串(字符串isbn、字符串作者、字符串标题、字符串发布者、整数年、ushort页面、十进制价格)
{
INinjectModule module=…;//创建一个模块并在此处添加ILogger
使用(IKernel内核=新的标准内核(模块))
{
var fakeLogger=kernel.Get();//获取记录器
var book=新书(isbn、作者、标题、出版商、年份、页数、价格、fakeLogger);
Console.WriteLine(book.FormatBook(book.Author,book.Publisher));
返回book.FormatBook(book.Author、book.Publisher);
}
}
如前所述,在单元测试中,通常不需要使用容器。但关于更一般的问题:
如何将参数传递给构造函数
通过这种方式,您可以:
kernel.Get<Book>(
new ConstructorArgument("isbn", "123"),
new ConstructorArgument("author", "XYZ")
...
);
kernel.Get(
新的构造论证(“isbn”、“123”),
新的构造器论证(“作者”、“XYZ”)
...
);
内核的设置在哪里?想想看,在单元测试中不需要以这种方式注入任何东西。您只需使用模拟对象作为记录器。