Database Reference
In-Depth Information
public void SaveChanges()
{
_context.SaveChanges();
}
public IQueryable<Book> BooksByCategory(string name)
{
return _context.Books.Where(b => b.Category.Name == name);
}
public IQueryable<Book> BooksByYear(int year)
{
return _context.Books.Where(b => b.PublishDate.Year == year);
}
}
}
5.
Right-click the solution, and select Add New Project. Select Test Project from the installed
templates. Add a reference to System.Data.Entity and a project reference to BookRepository .
6.
Right-click the Test project, and select Add New Test. Add a Unit Test to the Test project.
Add the code in Listing 8-19 to create the tests.
Listing 8-19. BookRepositoryTest Class with the Unit Tests
[TestClass]
public class BookRepositoryTest
{
private TestEntities _context;
[ClassInitialize]
public void TestSetup()
{
_context = new TestEntities();
if (_context.DatabaseExists())
{
_context.DeleteDatabase();
}
_context.CreateDatabase();
}
[TestMethod]
public void TestsBooksInCategory()
{
var repository = new BookRepository.BookRepository(_context);
var construction = new Category { Name = "Construction" };
var book = new Book { Title = "Building with Masonary",
Author = "Dick Kreh",
PublishDate = new DateTime(1998, 1, 1) };
book.Category = construction;
repository.InsertCategory(construction);
repository.InsertBook(book);
repository.SaveChanges();
 
Search WWH ::




Custom Search