コード例 #1
0
        public void Singleton_Instantiated_CallsConstructerWithKey()
        {
            string key = Random.Next().ToString();
            TestSingletonWithKeyedConstructor <string> testSingleton = TestSingletonWithKeyedConstructor <string> .Get(key);

            // The test class used here will set the value of the public field myKey from the key supplied to its constructer
            Assert.AreEqual(
                key,
                testSingleton.myKey,
                "When an instance of a singleton is created using GetSingleton, the constructor should be called with the key supplied as a parameter.");
        }
コード例 #2
0
        public void Singleton_WithDuplicateKeys_AreIdentical()
        {
            int key = Random.Next();
            TestSingletonWithKeyedConstructor <int> testSingleton1 = TestSingletonWithKeyedConstructor <int> .Get(key);

            TestSingletonWithKeyedConstructor <int> testSingleton2 = TestSingletonWithKeyedConstructor <int> .Get(key);

            TestSingletonWithKeyedConstructor <int> testSingleton3 = TestSingletonWithKeyedConstructor <int> .Get(key);

            Assert.AreSame(
                testSingleton1,
                testSingleton2,
                "Creating multiple singletons with the same key should result in the same object each time.");
            Assert.AreSame(
                testSingleton2,
                testSingleton3,
                "Creating multiple singletons with the same key should result in the same object each time.");
        }