 
 final class UserDefaultsObservationTests: XCTestCase {
     
-    private enum Clarus: Int {
-        
-        case dog, cow
-    }
-    
-    
-    private static let key = DefaultKey<Bool>("TestKey")
-    private static let initialEmssionkey = DefaultKey<Bool>("InitialEmissionKey")
-    private static let optionalKey = DefaultKey<String?>("OptionalTestKey")
-    private static let rawRepresentableKey = RawRepresentableDefaultKey<Clarus>("RawRepresentableTestKey")
-    
-    
-    override func tearDown() {
-        
-        super.tearDown()
-        
-        UserDefaults.standard.restore(key: Self.key)
-        UserDefaults.standard.restore(key: Self.initialEmssionkey)
-        UserDefaults.standard.restore(key: Self.optionalKey)
-        UserDefaults.standard.restore(key: Self.rawRepresentableKey)
-    }
-    
-    
     func testKeyObservation() {
         
-        let expectation = self.expectation(description: "UserDefaults observation")
+        let key = DefaultKey<Bool>("Test Key")
+        defer { UserDefaults.standard.restore(key: key) }
         
-        UserDefaults.standard[Self.key] = false
+        let expectation = self.expectation(description: "UserDefaults observation for normal key")
         
-        let observer = UserDefaults.standard.publisher(for: Self.key)
+        UserDefaults.standard[key] = false
+        
+        let observer = UserDefaults.standard.publisher(for: key)
             .sink { (value) in
                 XCTAssertTrue(value)
                 XCTAssertEqual(OperationQueue.current, .main)

    
    func testOptionalKey() {
        
        XCTAssertNil(UserDefaults.standard[Self.optionalKey])
        
        UserDefaults.standard[Self.optionalKey] = "cow"
        XCTAssertEqual(UserDefaults.standard[Self.optionalKey], "cow")
        
        let expectation = self.expectation(description: "UserDefaults observation")
        let observer = UserDefaults.standard.publisher(for: Self.optionalKey)
            .sink { (value) in
                XCTAssertNil(value)
                expectation.fulfill()
            }
        
        UserDefaults.standard[Self.optionalKey] = nil
        self.wait(for: [expectation], timeout: .zero)
        
        XCTAssertNil(UserDefaults.standard[Self.optionalKey])
        
        observer.cancel()
        UserDefaults.standard[Self.optionalKey] = "dog"
        XCTAssertEqual(UserDefaults.standard[Self.optionalKey], "dog")
    }
    
    
    func testRawRepresentable() {
        
        let expectation = self.expectation(description: "UserDefaults observation")
        
        UserDefaults.standard[Self.rawRepresentableKey] = .dog
        
        let observer = UserDefaults.standard.publisher(for: Self.rawRepresentableKey)
            .sink { (value) in
                XCTAssertEqual(value, .cow)
                expectation.fulfill()
            }
        
        UserDefaults.standard[Self.rawRepresentableKey] = .cow
        self.wait(for: [expectation], timeout: .zero)
        
        observer.cancel()
        UserDefaults.standard[Self.rawRepresentableKey] = .dog
        XCTAssertEqual(UserDefaults.standard[Self.rawRepresentableKey], .dog)
    }
    
}

                expectation.fulfill()
            }
        
        UserDefaults.standard[key] = true
        self.wait(for: [expectation], timeout: .zero)
        // -> Waiting with zero timeout can be failed when the closure is performed not immediately but in another runloop.
        
        observer.cancel()
        UserDefaults.standard[key] = false
    }
    
    
    func testInitialEmission() {
        
        let key = DefaultKey<Bool>("Initial Emission Test Key")
        defer { UserDefaults.standard.restore(key: key) }
        
        let expectation = self.expectation(description: "UserDefaults observation for initial emission")
        
        UserDefaults.standard[key] = false
        
        let observer = UserDefaults.standard.publisher(for: key, initial: true)
            .sink { (value) in
                XCTAssertFalse(value)
                expectation.fulfill()
            }
        
        observer.cancel()
        UserDefaults.standard[key] = true
        
        self.wait(for: [expectation], timeout: .zero)
    }
