C# 功能在现场工作,但在单元测试时不工作
我正在尝试运行一些单元测试,我需要做C# 功能在现场工作,但在单元测试时不工作,c#,asp.net,entity-framework,unit-testing,C#,Asp.net,Entity Framework,Unit Testing,我正在尝试运行一些单元测试,我需要做[TestInitialize]的事情 我在网上找到了一些信息。我不知道如何修复它,因为它对我来说毫无意义 [TestInitialize] public void TestInitialize() { mock = new Mock<ILibraryRepository>(); books = new List<Book>() { new Book { Id = 0, Title = "Tit
[TestInitialize]
的事情
我在网上找到了一些信息。我不知道如何修复它,因为它对我来说毫无意义
[TestInitialize]
public void TestInitialize()
{
mock = new Mock<ILibraryRepository>();
books = new List<Book>()
{
new Book { Id = 0, Title = "Title 0", Edition = 0, PublicationDate = DateTime.Now, Author = { Id = 0, Name = "Author 0", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 0 },
new Book { Id = 1, Title = "Title 1", Edition = 1, PublicationDate = DateTime.Now, Author = { Id = 1, Name = "Author 1", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 1 },
new Book { Id = 2, Title = "Title 2", Edition = 2, PublicationDate = DateTime.Now, Author = { Id = 2, Name = "Author 2", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 2 }
};
mock.Setup(m => m.Books).Returns(books.AsQueryable());
var controller = new BooksController(mock.Object);
}
我得到以下错误:
对象引用未设置为对象的实例
这是清楚的。它是空的。但我不明白为什么它是空的
调试说明:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Assignment2.Models.Book.Author.get returned null.
这对我来说毫无意义,因为当我启动程序时,这个函数起作用
图书
模型如下:
public class Book
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Display(Name = "Publication date")]
public DateTime? PublicationDate { get; set; }
public float? Edition { get; set; } // We might have a 2.5 edition. Rare but happens
public int AuthorId { get; set; }
public Author Author { get; set; }
}
我不知道它是否有用,但我的ILibraryRepository
在这里:
public interface ILibraryRepository
{
IQueryable<Book> Books { get; }
IQueryable<Author> Authors { get; }
Book Save(Book book);
Author Save(Author author);
void Delete(Book book);
void Delete(Author author);
}
公共接口ILibraryRepository
{
易读书籍{get;}
IQueryable作者{get;}
图书保存;
作者保存(Author-Author);
作废删除(图书);
无效删除(作者);
}
知道我为什么会犯这个错误吗
编辑:我将
HomeController
更改为BookController
。我不知道为什么我把这两者混在一起。错误发生在[TestInitialize]
部分您的初始化方法正好为零。基本上,在初始化方法“var controller”中,您需要花费大量的精力将一个mock分配给一个临时的局部变量,而您在测试中没有使用这个方法。如果这还不够的话,你的测试方法使用的甚至是一个不同的控制器。您需要这样做:
private HomeController controller;
[TestInitialize]
public void TestInitialize()
{
mock = new Mock<ILibraryRepository>();
books = new List<Book>()
{
new Book { Id = 0, Title = "Title 0", Edition = 0, PublicationDate = DateTime.Now, Author = { Id = 0, Name = "Author 0", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 0 },
new Book { Id = 1, Title = "Title 1", Edition = 1, PublicationDate = DateTime.Now, Author = { Id = 1, Name = "Author 1", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 1 },
new Book { Id = 2, Title = "Title 2", Edition = 2, PublicationDate = DateTime.Now, Author = { Id = 2, Name = "Author 2", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 2 }
};
mock.Setup(m => m.Books).Returns(books.AsQueryable());
controller = new HomeController(mock.Object);
}
[TestMethod]
public void IndexLoadsValid()
{
// Arrange - use arrange from test initialise
// Act
var result = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
私人家庭控制器;
[测试初始化]
public void TestInitialize()
{
mock=新mock();
图书=新列表()
{
新书{Id=0,Title=“Title 0”,Edition=0,PublicationDate=DateTime.Now,Author={Id=0,Name=“Author 0”,DateOfBirth=DateTime.Now,DateOfDeath=DateTime.Now},AuthorId=0},
新书{Id=1,Title=“Title 1”,Edition=1,PublicationDate=DateTime.Now,Author={Id=1,Name=“Author 1”,DateOfBirth=DateTime.Now,DateOfDeath=DateTime.Now},AuthorId=1},
新书{Id=2,Title=“Title 2”,Edition=2,PublicationDate=DateTime.Now,Author={Id=2,Name=“Author 2”,DateOfBirth=DateTime.Now,DateOfDeath=DateTime.Now},AuthorId=2}
};
Setup(m=>m.Books).Returns(Books.AsQueryable());
控制器=新的HomeController(mock.Object);
}
[测试方法]
public void IndexLoadsValid()
{
//排列-使用测试初始化中的排列
//表演
var result=controller.Index()作为ViewResult;
//断言
Assert.IsNotNull(结果);
}
这并不一定是最佳实践,但如果您希望测试使用initialise方法,您实际上需要对该方法的结果执行一些操作,而不是让它被垃圾收集。您使用了Author={Id=0,etc},这假设Books构造函数也实例化了Author,但它仍然为null。如果您需要异常帮助,请包括stacktrace,正如@PaulAbbott所说:
Author={Id=0…
应该是Author=new Author(){Id=0…
我不知道你的模拟存储库是如何进入HomeController
的。这不是吗?mock=new mock();
他的意思是你需要var controller=new HomeController(mock.Object)之类的东西
否则主控制器如何知道模拟存在?不应该Author={Id=0…
beAuthor=new Author(){Id=0…
?@PaulAbbott你想添加一个答案,这样我就可以选择最好的答案了吗?该死的,我今天很笨…谢谢你抓住了。我编辑了这篇文章,但同样的错误仍然存在。错误发生在[TestInitialize]中
partIt将出现在代码的使用部分,而不是测试代码中。我在测试代码中看不出发生这种情况的任何原因。可能控制器中的代码有其他依赖项,这些依赖项不是由测试代码初始化的。Assignment2是什么?它是从控制器构造函数直接静态调用的吗?它是已在评论中解决。不过感谢您的帮助!
private HomeController controller;
[TestInitialize]
public void TestInitialize()
{
mock = new Mock<ILibraryRepository>();
books = new List<Book>()
{
new Book { Id = 0, Title = "Title 0", Edition = 0, PublicationDate = DateTime.Now, Author = { Id = 0, Name = "Author 0", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 0 },
new Book { Id = 1, Title = "Title 1", Edition = 1, PublicationDate = DateTime.Now, Author = { Id = 1, Name = "Author 1", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 1 },
new Book { Id = 2, Title = "Title 2", Edition = 2, PublicationDate = DateTime.Now, Author = { Id = 2, Name = "Author 2", DateOfBirth = DateTime.Now, DateOfDeath = DateTime.Now }, AuthorId = 2 }
};
mock.Setup(m => m.Books).Returns(books.AsQueryable());
controller = new HomeController(mock.Object);
}
[TestMethod]
public void IndexLoadsValid()
{
// Arrange - use arrange from test initialise
// Act
var result = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(result);
}