コード例 #1
0
        public void TestThatExceptionIsThrownWhenStubIsNotSetup()
        {
            var        stub      = new StubIPhoneBook();
            IPhoneBook phoneBook = stub;

            phoneBook.GetContactPhoneNumber("John", "Smith");
        }
コード例 #2
0
ファイル: PhoneBook.cs プロジェクト: eitanbehar/learning
 public PhoneBook(IPhoneBook myPhoneBook)
 {
     // IPhoneBook is the "target"
     // Two adapters can be used: ListAdapter, and FlatAdapter
     // Adapter is injected
     this.myPhoneBook = myPhoneBook;
 }
コード例 #3
0
        public async Task TestMethod_WithReturnType_WithParameters_DefaultBehavior_Strict_Async()
        {
            var        stub      = new StubIPhoneBook(MockBehavior.Strict);
            IPhoneBook phoneBook = stub;

            Assert.AreEqual(0, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
        }
コード例 #4
0
        public void SavePhoneBookTest()
        {
            PhoneBookCreator creator = new PhoneBookCreator();
            IPhoneBook       book    = creator.CreateNewPhoneBook();

            string filePath = "File.bin";

            book.CreatePhoneBookEntry("Gosho", "Goshov");

            Assert.IsFalse(File.Exists(filePath));

            creator.SavePhoneBook(filePath, book);

            Assert.IsTrue(File.Exists(filePath));

            IPhoneBook loadingBook = creator.LoadPhoneBook(filePath);

            Assert.IsNotEmpty(loadingBook.GetPhoneBookEntriesSortedByFirstName());

            Assert.IsTrue(loadingBook.GetPhoneBookEntriesSortedByFirstName().Count == 1);

            Assert.AreEqual("Gosho", loadingBook.GetPhoneBookEntriesSortedByFirstName()[0].FirstName);

            File.Delete(filePath);
        }
コード例 #5
0
ファイル: IPhoneBook.cs プロジェクト: vl85/AopCodeGeneration
 public PhoneBookAopProxy(
     IPhoneBook realInstance,
     InterceptionHandler interceptionHandler)
 {
     _realInstance = realInstance;
     _interceptionHandler = interceptionHandler;
 }
コード例 #6
0
        public void TestMethod_WithReturnType_WithParameters_DefaultBehavior_Loose()
        {
            var        stub      = new StubIPhoneBook(MockBehavior.Loose);
            IPhoneBook phoneBook = stub;

            Assert.AreEqual(0, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
コード例 #7
0
        public static void Import(this IPhoneBook book, FileStream stream, IPhonebookImporter importer = null)
        {
            importer = importer ?? _importer;
            var newBook = importer.Import <PhoneBook>(stream);

            book.AddRange(newBook);
        }
コード例 #8
0
        public async Task TestThatDefaultBehaviorIsUsedWhenStubIsNotSetup_Async()
        {
            var        stub      = new StubIPhoneBook();
            IPhoneBook phoneBook = stub;
            var        result    = await phoneBook.GetContactPhoneNumberAsync("John", "Smith");

            Assert.AreEqual(0, result);
        }
コード例 #9
0
        public void TestThatExceptionIsThrownWhenMethodIsCalledMoreThanExpected()
        {
            var        stub      = new StubIPhoneBook().GetContactPhoneNumber((p1, p2) => 12345678, Times.Once);
            IPhoneBook phoneBook = stub;

            phoneBook.GetContactPhoneNumber("John", "Smith");
            phoneBook.GetContactPhoneNumber("John", "Smith");
        }
コード例 #10
0
        public void TestPropertyStubWithGetterOnly()
        {
            int        contactsCount = 55;
            var        stub          = new StubIPhoneBook().ContactsCount_Get(() => contactsCount);
            IPhoneBook phoneBook     = stub;

            Assert.AreEqual(contactsCount, phoneBook.ContactsCount);
        }
コード例 #11
0
        public void TestThatDefaultBehaviorIsUsedWhenStubIsNotSetup()
        {
            var        stub      = new StubIPhoneBook();
            IPhoneBook phoneBook = stub;
            var        result    = phoneBook.GetContactPhoneNumber("John", "Smith");

            Assert.AreEqual(0, result);
        }
コード例 #12
0
        public void GenerateAndApplyReversePatch()
        {
            var initial = new PhoneBook()
            {
                Entries = new Dictionary <string, TestPerson>()
                {
                    ["0800JOHNDOE"] = new TestPerson()
                    {
                        FirstName = "John",
                        LastName  = "Doe",
                        IsAdult   = true,
                    },
                    ["0800JANEDOE"] = new TestPerson()
                    {
                        FirstName = "Jane",
                        LastName  = "Doe",
                        IsAdult   = true,
                    },
                },
            };

            using DraftScope scope = (DraftScope)DraftExtensions.CreateDraft(initial, out PhoneBook draft);

            var patchGenerator = new DictionaryPatchGenerator();
            var patches        = new JsonPatchDocument();
            var inversePatches = new JsonPatchDocument();

            draft.Entries.Remove("0800JANEDOE");
            draft.Entries.Add("0800BABYDOE", new TestPerson()
            {
                FirstName = "Baby",
                LastName  = "Doe",
            });

            // trick the scope into thinking that is finishing and should not create proxies anymore.
            scope.IsFinishing = true;

            patchGenerator.Generate((IDraft)draft.Entries, "/Entries", patches, inversePatches);

            // inverse order of inverse patches.
            inversePatches.Operations.Reverse();

            var final = scope.FinishDraft <PhoneBook, IPhoneBook>(draft);

            var result = IPhoneBook.Produce(initial, p =>
            {
                patches.ApplyTo(p);
            });

            result = result.Produce(p =>
            {
                inversePatches.ApplyTo(p);
            });

            Assert.Equal(2, result.Entries.Count);
            Assert.Same(initial.Entries["0800JOHNDOE"], result.Entries["0800JOHNDOE"]);
            Assert.Same(initial.Entries["0800JANEDOE"], result.Entries["0800JANEDOE"]);
        }
コード例 #13
0
        public void TestThatLooseMockBehaviorsAreAlwaysOverwritten()
        {
            var stub = new StubIPhoneBook(MockBehavior.Loose)
                       .GetContactPhoneNumber((p1, p2) => 12345678);

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(12345678, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
コード例 #14
0
        public async Task TestThatExceptionIsThrownWhenMethodIsCalledMoreThanExpected_Async()
        {
            var stub = new StubIPhoneBook()
                       .GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(12345678), Times.Once);
            IPhoneBook phoneBook = stub;
            await phoneBook.GetContactPhoneNumberAsync("John", "Smith");

            await phoneBook.GetContactPhoneNumberAsync("John", "Smith");
        }
コード例 #15
0
        public async Task TestThatLooseMockBehaviorsAreAlwaysOverridden_Async()
        {
            var stub = new StubIPhoneBook(MockBehavior.Loose)
                       .GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(12345678));

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(12345678, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
        }
コード例 #16
0
        public AddEditViewModel(ViewModel viewModel)
        {
            mainModel = viewModel;

            pbook           = viewModel.PhoneBook;
            form            = viewModel.Form;
            selectedContact = viewModel.SelectedContact;

            selectedContactModel = viewModel.SelectedContactModel;
        }
コード例 #17
0
ファイル: PhoneBookCreator.cs プロジェクト: gtochev/Validata
        public void SavePhoneBook(string filePathAndFileName, IPhoneBook book)
        {
            System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();

            Stream stream = new FileStream(filePathAndFileName,
                                     FileMode.Create,
                                     FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, book);
            stream.Close();
        }
コード例 #18
0
        public void TestThatMethodStubCanBeOverwritten()
        {
            var stub = new StubIPhoneBook().GetContactPhoneNumber((p1, p2) => 12345678);

            stub.GetContactPhoneNumber((p1, p2) => 11122233, overwrite: true);

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(11122233, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
コード例 #19
0
 public PhoneBookController(IPhoneBook _service)
 {
     service = _service;
     using (var db = new DatabaseContext())
     {
         uow = new UnitOfWork(db);
         _phoneBookRepository = uow.GetRepository <PhoneBook>();
         _userRepository      = uow.GetRepository <User>();
     }
 }
コード例 #20
0
        public async Task TestThatMethodStubCanBeOverwritten_Async()
        {
            var stub = new StubIPhoneBook().
                       GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(12345678));

            stub.GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(11122233), overwrite: true);

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(11122233, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
        }
コード例 #21
0
        public void SavePhoneBook(string filePathAndFileName, IPhoneBook book)
        {
            System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();

            Stream stream = new FileStream(filePathAndFileName,
                                           FileMode.Create,
                                           FileAccess.Write, FileShare.None);

            formatter.Serialize(stream, book);
            stream.Close();
        }
コード例 #22
0
        public AddEditViewModel(ViewModel viewModel)
        {
            mainModel = viewModel;

            pbook                = viewModel.PhoneBook;
            form                 = viewModel.Form;
            selectedContact      = viewModel.SelectedContact;
            selectedDepartment   = viewModel.SelectedDepartment;
            selectedContactModel = viewModel.SelectedContactModel;

            departments = pbook.GetAllDepartments();
        }
コード例 #23
0
        public IPhoneBook LoadPhoneBook(string filePathAndFileName)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream     stream    = new FileStream(filePathAndFileName,
                                                  FileMode.Open,
                                                  FileAccess.Read,
                                                  FileShare.Read);
            IPhoneBook book = (IPhoneBook)formatter.Deserialize(stream);

            stream.Close();
            return(book);
        }
コード例 #24
0
        public void TestPropertyStubWithGetterAndSetter()
        {
            long myNumber  = 6041234567;
            long newNumber = 0;

            var stub = new StubIPhoneBook()
                       .MyNumber_Get(() => myNumber)
                       .MyNumber_Set(value => newNumber = value);

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(myNumber, phoneBook.MyNumber);
            phoneBook.MyNumber = 13;
            Assert.AreEqual(13, newNumber);
        }
コード例 #25
0
        public void TestCallSequence()
        {
            var stub = new StubIPhoneBook()
                       .GetContactPhoneNumber((p1, p2) => 12345678, Times.Once)     // first call
                       .GetContactPhoneNumber((p1, p2) => 11122233, Times.Twice)    // next two calls
                       .GetContactPhoneNumber((p1, p2) => 22233556, Times.Forever); // rest of the calls

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(12345678, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(11122233, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(11122233, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(22233556, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(22233556, phoneBook.GetContactPhoneNumber("John", "Smith"));
            Assert.AreEqual(22233556, phoneBook.GetContactPhoneNumber("John", "Smith"));
        }
コード例 #26
0
        public async Task TestCallSequence_Async()
        {
            var stub = new StubIPhoneBook()
                       .GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(12345678), Times.Once)     // first call
                       .GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(11122233), Times.Twice)    // next two calls
                       .GetContactPhoneNumberAsync(async(p1, p2) => await Task.FromResult(22233556), Times.Forever); // rest of the calls

            IPhoneBook phoneBook = stub;

            Assert.AreEqual(12345678, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
            Assert.AreEqual(11122233, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
            Assert.AreEqual(11122233, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
            Assert.AreEqual(22233556, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
            Assert.AreEqual(22233556, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
            Assert.AreEqual(22233556, await phoneBook.GetContactPhoneNumberAsync("John", "Smith"));
        }
コード例 #27
0
 public void Export(Stream stream, IPhoneBook book)
 {
     if (book == null)
     {
         throw new ArgumentException("phone book should be not null");
     }
     if (stream == null)
     {
         throw new ArgumentException("stream should be not null");
     }
     if (!stream.CanWrite)
     {
         throw new ArgumentException("stream is not availible to write");
     }
     foreach (var contact in book)
     {
         SerializeContact(stream, contact);
     }
 }
コード例 #28
0
        public async Task TestMethod_WithReturnType_WithParameters_Async()
        {
            long   number    = 6041234567;
            string firstName = null;
            string lastName  = null;
            var    stub      = new StubIPhoneBook();

            stub.GetContactPhoneNumberAsync(async(fn, ln) =>
            {
                firstName = fn;
                lastName  = ln;
                return(await Task.FromResult(number));
            });
            IPhoneBook phoneBook    = stub;
            long       actualNumber = await phoneBook.GetContactPhoneNumberAsync("John", "Smith");

            Assert.AreEqual(number, actualNumber);
            Assert.AreEqual("John", firstName);
            Assert.AreEqual("Smith", lastName);
        }
コード例 #29
0
        public void TestMethod_WithReturnType_WithParameters()
        {
            long   number    = 6041234567;
            string firstName = null;
            string lastName  = null;
            var    stub      = new StubIPhoneBook();

            stub.GetContactPhoneNumber((fn, ln) =>
            {
                firstName = fn;
                lastName  = ln;
                return(number);
            });
            IPhoneBook phoneBook    = stub;
            long       actualNumber = phoneBook.GetContactPhoneNumber("John", "Smith");

            Assert.AreEqual(number, actualNumber);
            Assert.AreEqual("John", firstName);
            Assert.AreEqual("Smith", lastName);
        }
コード例 #30
0
 public void Export(Stream stream,
                    IPhoneBook book)
 {
     if (book == null)
     {
         throw new ArgumentException("phone book should be not null");
     }
     if (stream == null)
     {
         throw new ArgumentException("stream should be not null");
     }
     if (!stream.CanWrite)
     {
         throw new ArgumentException("stream is not availible to write");
     }
     foreach (var contact in book)
     {
         MessagePackSerializer.Serialize(stream, contact, ContractlessStandardResolver.Instance);
     }
 }
コード例 #31
0
 public void Init()
 {
     service = new PhoneBookService();
 }