using NHibernate; using Northwind.DataAccess; using Northwind.DataAccess.Repository; using Northwind.Domain; using Northwind.Domain.Model; using Northwind.Services; using Northwind.TransferObjects.Model; using Northwind.TransferObjects.Presentation; using NMock2; using NUnit.Framework; using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace Northwind.Services.Test { [TestFixture] public class CustomerServicesTest { private Mockery mockery; private CustomerServices customerServices; private IInMemoryCustomerRepository mockCustomerRepository; private ISessionFactory mockSessionFactory; private ISession mockSession; private ITransaction mockTransaction; [SetUp] public void Setup() { mockery = new Mockery(); mockCustomerRepository = mockery.NewMock<IInMemoryCustomerRepository>(); mockSessionFactory = mockery.NewMock<ISessionFactory>(); mockSession = mockery.NewMock<ISession>(); mockTransaction = mockery.NewMock<ITransaction>(); customerServices = new CustomerServices(mockCustomerRepository, mockSessionFactory); } [Test] public void GetDetailsForCustomerTest() { Customer customer = new Customer(); customer.ID = "ABC"; customer.CompanyName = "NEW COMPANY"; Expect.Once.On(mockCustomerRepository).Method("Load").With(customer.ID).Will(Return.Value(customer)); CustomerTO cTO = customerServices.GetDetailsForCustomer(customer.ID); Assert.AreEqual(cTO.CompanyName, customer.CompanyName); } [Test] public void UpdateCustomerTest() { Customer customer = new Customer(); customer.ID = "ABC"; customer.CompanyName = "NEW COMPANY"; customer.Phone = "555-1234"; CustomerTO updatedTO = (CustomerTO)TOHelper<Customer>.GetTO(typeof(CustomerTO), customer); Expect.Once.On(mockCustomerRepository).Method("Load").With(updatedTO.ID).Will(Return.Value(customer)); Expect.Once.On(mockSessionFactory).Method("OpenSession").WithNoArguments().Will(Return.Value(mockSession)); Expect.Once.On(mockSession).Method("BeginTransaction").WithNoArguments().Will(Return.Value(mockTransaction)); Expect.Once.On(mockCustomerRepository).Method("AddOrUpdate").With(mockSession, customer); Expect.Once.On(mockTransaction).Method("Commit").WithNoArguments(); Expect.Once.On(mockTransaction).Method("Dispose").WithNoArguments(); Expect.Once.On(mockSession).Method("Dispose").WithNoArguments(); customerServices.UpdateCustomer(updatedTO); } [Test] public void NewCustomerTest() { Customer customer = new Customer(); customer.ID = "NEW"; Expect.Once.On(mockSessionFactory).Method("OpenSession").WithNoArguments().Will(Return.Value(mockSession)); Expect.Once.On(mockSession).Method("BeginTransaction").WithNoArguments().Will(Return.Value(mockTransaction)); Expect.Once.On(mockCustomerRepository).Method("AddOrUpdate").With(mockSession, customer); Expect.Once.On(mockTransaction).Method("Commit").WithNoArguments(); Expect.Once.On(mockTransaction).Method("Dispose").WithNoArguments(); Expect.Once.On(mockSession).Method("Dispose").WithNoArguments().Will(); CustomerTO to = customerServices.NewCustomer(customer.ID); Assert.AreEqual(customer.ID, to.ID); } [Test] public void DeleteCustomerTest() { Customer custToDelete = new Customer(); custToDelete.ID = "TODEL"; Expect.Once.On(mockSessionFactory).Method("OpenSession").WithNoArguments().Will(Return.Value(mockSession)); Expect.Once.On(mockSession).Method("BeginTransaction").WithNoArguments().Will(Return.Value(mockTransaction)); Expect.Once.On(mockCustomerRepository).Method("Remove").With(mockSession, custToDelete); Expect.Once.On(mockTransaction).Method("Commit").WithNoArguments(); Expect.Once.On(mockTransaction).Method("Dispose").WithNoArguments(); Expect.Once.On(mockSession).Method("Dispose").WithNoArguments().Will(); customerServices.DeleteCustomer(custToDelete.ID); } [TearDown] public void TearDown() { mockery.VerifyAllExpectationsHaveBeenMet(); } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)