swiftunit-testingswift-composable-architecturetca

Unable to complete setting up the CounterFeatureTests class


I am following the guide here in the Getting started document to complete the Testing for your feature module, but I keep getting the following exceptions even if my feature struct conforms to the Reducer and my State implements the Equatable protocol.

Cannot convert value of type 'CounterFeature.State' to expected argument tpye '_'

Initializer 'init(initialState:reducer:withDependencies:fileID:file:line:column:)' requires that 'CounterFeature' conform to 'Reducer'

Kindly assist, here is the part of my CounterFeature struct that indicates conformance.

import ComposableArchitecture
import SwiftUI

@Reducer
struct CounterFeature {
    @ObservableState
    struct State: Equatable {
        var count = 0
    }

    enum Action {
        case decrementButtonTapped
        case incrementButtonTapped
    }
    ...
}

The Test class setup here

@testable import TCALearning
import ComposableArchitecture
import Testing


@MainActor
struct CounterFeatureTests {
    @Test
    func basics() async {
        let store = TestStore(initialState: CounterFeature.State()) {
            CounterFeature()
        }
    }
}

Solution

  • I was able to resolve this by making a couple of changes to my setup as follows

    1. Ensure that you have no typos in your tests or code.

    2. Check the target membership for the files under test. If a file under test isn't included in the test bundle, you will encounter issues like I did. To do this, if you can't remember if you did it at the point of creating your files, you can follow these steps:

      a. Click on the file in the Xcode navigator

      b. Check the Target Membership in the right-hand utility pane.

      c. Ensure your test target is selected

    3. Ensure you do not have the ComposableAchitecture dependency in your Link Binary With Libraries, as this should be automatically applied from your app.

    4. Finally, once you complete the above steps, ensure to clean your build folder. Once you are done cleaning, try to run your tests again; This should trigger a full build of the necessary files.