Advertisement
Guest User

Untitled

a guest
Jul 7th, 2010
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Rhino.Mocks;
  4.  
  5. namespace TestProject1
  6. {
  7.     /// <summary>
  8.     /// Summary description for UnitTest8
  9.     /// </summary>
  10.     [TestClass]
  11.     public class UnitTest8
  12.     {
  13.         private const int ValidId = 4711;
  14.         private const int InvalidId = 0;
  15.         private MockRepository mockRepository;
  16.         private ISessionFactory sessionFactory;
  17.         private ISession session;
  18.         private LockClassPanelController controller;
  19.  
  20.         /// <summary>
  21.         ///Gets or sets the test context which provides
  22.         ///information about and functionality for the current test run.
  23.         ///</summary>
  24.         public TestContext TestContext { get; set; }
  25.  
  26.         [TestInitialize]
  27.         public void Setup()
  28.         {
  29.             mockRepository = new MockRepository();
  30.             sessionFactory = mockRepository.StrictMock<ISessionFactory>();
  31.             session = mockRepository.StrictMock<ISession>();
  32.         }
  33.  
  34.  
  35.         #region Additional test attributes
  36.         //
  37.         // You can use the following additional attributes as you write your tests:
  38.         //
  39.         // Use ClassInitialize to run code before running the first test in the class
  40.         // [ClassInitialize()]
  41.         // public static void MyClassInitialize(TestContext testContext) { }
  42.         //
  43.         // Use ClassCleanup to run code after all tests in a class have run
  44.         // [ClassCleanup()]
  45.         // public static void MyClassCleanup() { }
  46.         //
  47.         // Use TestInitialize to run code before running each test
  48.         // [TestInitialize()]
  49.         // public void MyTestInitialize() { }
  50.         //
  51.         // Use TestCleanup to run code after each test has run
  52.         // [TestCleanup()]
  53.         // public void MyTestCleanup() { }
  54.         //
  55.         #endregion
  56.  
  57.         [TestMethod]
  58.         public void AddLockClassWithNullNameShouldCallInsertOnSessionWithEmptyString()
  59.         {
  60.             LockClass lockClass = new LockClass { Id = ValidId, Name = null };
  61.  
  62.             using (mockRepository.Record())
  63.             {
  64.                 sessionFactory.CreateSession();
  65.                 LastCall.Return(session);
  66.  
  67.                 session.InsertWithId(lockClass);
  68.                 LastCall.Return(lockClass);
  69.  
  70.                 session.Commit();
  71.                 session.Dispose();
  72.             }
  73.  
  74.             using (mockRepository.Playback())
  75.             {
  76.                 controller = new LockClassPanelController(sessionFactory);
  77.                 controller.AddLockClass(lockClass.Id, string.Empty);
  78.             }
  79.  
  80.             mockRepository.VerifyAll();
  81.         }
  82.     }
  83.  
  84.     internal class LockClassPanelController
  85.     {
  86.         private readonly ISessionFactory sessionFactory;
  87.  
  88.         public LockClassPanelController(ISessionFactory sessionFactory)
  89.         {
  90.             this.sessionFactory = sessionFactory;
  91.         }
  92.  
  93.         public void AddLockClass(int id, string name)
  94.         {
  95.             if (id != 0)
  96.             {
  97.                 using (var session = sessionFactory.CreateSession())
  98.                 {
  99.                     session.InsertWithId(new LockClass { Id = id, Name = name });
  100.                     session.Commit();
  101.                 }
  102.                 //LoadLockClasses();
  103.                 //view.Initialize();
  104.             }
  105.         }
  106.     }
  107.  
  108.     public interface ISession : IDisposable
  109.     {
  110.         LockClass InsertWithId(LockClass lockClass);
  111.         void Commit();
  112.         void Dispose();
  113.     }
  114.  
  115.     public interface ISessionFactory
  116.     {
  117.         ISession CreateSession();
  118.     }
  119.  
  120.     public class LockClass
  121.     {
  122.         public int Id { get; set; }
  123.         public string Name { get; set; }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement