1
\$\begingroup\$

I am currently implementing an app that uses an architecture based on features & usecases and message sending.

Features are created with message handler passed in.
To test the feature, a handler intercepting the resulting message, must be passed in.

I came up with 2 solutions, both currently being present in the project's code:

  1. use an object implementing the message handling protocol and offering a callback.

    import Quick
    import Nimble
    @testable import LibExample
    
    class Mock {
        class MessageHandler: MessageHandling {
    
           init(callBack: @escaping (Message) -> ()) {
                self.callBack = callBack
            }
    
            let callBack: (Message) -> ()
    
            func handle(msg: Message) {
                callBack(msg)
            }
        }
    }
    
    class SettingsFeatureSpec: QuickSpec {
        override func spec() {
            describe("the SettingsFeature UseCase") {
                var sut: SettingsFeature!
    
                var messageHandler: Mock.MessageHandler!
                var receivedModes: [TransportOption]!
    
                beforeEach {
                    receivedModes = []
                    messageHandler = Mock.MessageHandler {
                        if case .feature(.settings(.useCase(.transport(.action(.didActivate(let m)))))) = $0         {
                            receivedModes.append(m)
                        }
                    }
                    sut = SettingsFeature(with: messageHandler)
                }
    
                afterEach {
                    receivedModes = nil
                    messageHandler = nil
                    sut = nil
                }
    
                it("switches transport modes") {
                    sut.handle(feature: .settings(.useCase(.transport(.action(.activate(.udp))))))
                    sut.handle(feature: .settings(.useCase(.transport(.action(.activate(.tcp))))))
                    sut.handle(feature: .settings(.useCase(.transport(.action(.activate(.tls))))))
                    sut.handle(feature: .settings(.useCase(.transport(.action(.activate(.udp))))))
    
                    expect(receivedModes) == [.udp, .tcp, .tls, .udp]
                }
            }
        }
    }
    
  2. have the test/spec implement the the message handling

    import Quick
    import Nimble
    @testable import LibExample
    
    class UserHandlingSpec: QuickSpec, MessageHandling {
    
        var loggedInUser:User!
        var loggedOutUser:User!
    
        func handle(msg: Message) {
             switch msg {
            case .feature(.userHandling(.useCase(.login(.action(.logInConfirmed(let user)))))):
                self.loggedInUser = user
            case .feature(.userHandling(.useCase(.logout(.action(.logOutConfirmed(let user)))))):
                self.loggedOutUser = user
            default:
                break
            }
        }
    
        override func spec() {
            describe("the UserHandling Feature") {
                var sut: UserHandlingFeature!
    
                beforeEach {
                    sut = UserHandlingFeature(with: self)
                }
    
                afterEach {
                    sut = nil
                    self.loggedOutUser = nil
                    self.loggedInUser = nil
                }
    
                it("logs in user"){
                    sut.handle(feature: .userHandling(.useCase(.login(.action(.logIn("pinky", "password"))))))
    
                    expect(self.loggedInUser.name) == "pinky"
                }
    
                it("logs out user"){
                    sut.handle(feature: .userHandling(.useCase(.logout(.action(.logOut(User(name:"the brain")))))))
    
                    expect(self.loggedOutUser.name) == "the brain"
                }
            }
        }
    }
    

Which one would be the preferred way and why?

\$\endgroup\$
  • \$\begingroup\$ Have you tried either of these solutions? Please take a look at the help center. \$\endgroup\$ – Mast Oct 14 '19 at 16:19
  • \$\begingroup\$ @Mast: yes, I did \$\endgroup\$ – vikingosegundo Oct 14 '19 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.