Reswift: Testing middleware in main project

Created on 2 Dec 2018  路  3Comments  路  Source: ReSwift/ReSwift

Hi,

I'm currently working on a small personal project using ReSwift!
I want to tests what's happening inside my middlewares. However, since ReSwift framework disables testability for release builds, which makes sense (I integrated it via Carthage) I get the annoying error Module 'ReSwift' was not compiled for testing when I try to do @testable import ReSwift in my test target.

I tried just doing import ReSwift which doesn't generate any compilation errors but then my tests fail with no clear explanation why.
This however, could be because of the setup I have for them, which is nothing special I believe (I do my tests with Quick & Nimble)

Do you have any suggestions on how to overcome this problem?

Thanks in advance!

Most helpful comment

@danielmartinprieto @DivineDominion

Thanks for the quick answers! Yes, you pointed me to the correct direction. I had to link ReSwift to my test target (I have several targets) build phase & also add it to the copy files phase (all of this Carthage related).
That was the missing part, thanks again!

Just for the sake of completion I'll leave down below a sample of what I was originally trying out. Perhaps it can help others for future similar questions.

class fetchCardImagesSideEffectsSpec: QuickSpec {
    override func spec() {
        describe("fetchCardImagesSideEffects") {
            var middelwareItem: Middleware<AppState>!
            var urlSessionMock: SessionProtocolMock!
            var actionRecorder: [Action]!
            let dispatchFunction: DispatchFunction = { action in
                actionRecorder.append(action)
            }
            let getState: () -> AppState? = {
                return AppState.defaultState()
            }
            let nextAction: DispatchFunction = { _ in }

            beforeEach {
                urlSessionMock = SessionProtocolMock()
                actionRecorder = []
            }

            context("when dispatching a unrealated action") {
                beforeEach {
                    middelwareItem = fetchCardImagesSideEffects(session: urlSessionMock)
                }

                it("does nothing") {
                    let dispatchAction = middelwareItem(dispatchFunction, getState)(nextAction)
                    dispatchAction(CustomTestAction.some)
                    expect(actionRecorder.count) == 0
                }
            }
        }
    }
}

All 3 comments

The good thing about ReSwift middlewares is that they鈥檙e just functions, so you don鈥檛 need anything special or a complex setup in order to test them. You don鈥檛 even need a store. If you put an example of one of your middleware, we can try to test it together.

Sent with GitHawk

@Thurman1776- You're not supposed to need access to internals with the @testable annotation. import ReSwift is the way to go to make use of the API. (The tests of the API happen inside this repo here :)) -- So I guess there's something wrong with your tests. There must be some kind of failure reason.

@danielmartinprieto @DivineDominion

Thanks for the quick answers! Yes, you pointed me to the correct direction. I had to link ReSwift to my test target (I have several targets) build phase & also add it to the copy files phase (all of this Carthage related).
That was the missing part, thanks again!

Just for the sake of completion I'll leave down below a sample of what I was originally trying out. Perhaps it can help others for future similar questions.

class fetchCardImagesSideEffectsSpec: QuickSpec {
    override func spec() {
        describe("fetchCardImagesSideEffects") {
            var middelwareItem: Middleware<AppState>!
            var urlSessionMock: SessionProtocolMock!
            var actionRecorder: [Action]!
            let dispatchFunction: DispatchFunction = { action in
                actionRecorder.append(action)
            }
            let getState: () -> AppState? = {
                return AppState.defaultState()
            }
            let nextAction: DispatchFunction = { _ in }

            beforeEach {
                urlSessionMock = SessionProtocolMock()
                actionRecorder = []
            }

            context("when dispatching a unrealated action") {
                beforeEach {
                    middelwareItem = fetchCardImagesSideEffects(session: urlSessionMock)
                }

                it("does nothing") {
                    let dispatchAction = middelwareItem(dispatchFunction, getState)(nextAction)
                    dispatchAction(CustomTestAction.some)
                    expect(actionRecorder.count) == 0
                }
            }
        }
    }
}
Was this page helpful?
0 / 5 - 0 ratings