- Notifications
You must be signed in to change notification settings - Fork1
Testing helpers and extensions for SwiftRex
License
SwiftRex/TestingExtensions
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Testing helpers and extensions for SwiftRex and Combine
let(dependencies, scheduler)=self.setupassert( initialValue:AppState.initial, reducer:Reducer.myReducer.lift(action: \.actionReducerScope, state: \.stateReducerScope), middleware:MyHandler.middleware.inject(dependencies), steps:{Send(action:.certainAction(.verySpecific))SideEffectResult{ scheduler.advance(by:.seconds(5))}Send(action:.anotherAction(.withSomeValues(list:[], date: dependencies.now()))) // if you receive an action and don't add this block, the test will fail to remind youReceive{ action->Boolin // validates that the received action is what you would expect // if this function returns false, the test will fail to show you that you've got an unexpected actionif caselet.my(.expectedAction(list))= action{return list.isEmpty}else{returnfalse}}SideEffectResult{ scheduler.advance(by:.seconds(5))}Send(action:.anotherAction(.stopSomething)).expectStateToHaveChanged{ statein // if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here: state.somePropertyShouldHaveChangedTo=true // any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test}SideEffectResult{ scheduler.advance(by:.seconds(5))} // if you receive an action and don't add this block, the test will fail to remind youReceive{ action->Boolin // validates that the received action is what you would expect // if this function returns false, the test will fail to show you that you've got an unexpected actionif caselet.my(.expectedAction(list))= action{return list.isEmpty}else{returnfalse}}.expectStateToHaveChanged{ statein // if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here: state.somePropertyShouldHaveChangedTo=true // any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test}})
Result Builders (https://developer.apple.com/videos/play/wwdc2021/10253/) are awesome, however the diagnostics in case of type mismatch or failure toinfer the type expression are misleading, incomplete or confusing. Although the Swift Core Team keeps improving the diagnostics every release, you mayeventually find yourself in a compiler error that is tricky to understand. In that case, you may want to use the old format, that use plain arrays.
// Instead of:assert( // ... steps:{Send(action:.certainAction(.verySpecific))SideEffectResult{ scheduler.advance(by:.seconds(5))}})// You can use:assert( // ... steps:[Send(action:.certainAction(.verySpecific)).asStep,SideEffectResult{ scheduler.advance(by:.seconds(5))}.asStep])
For that:
- replace
steps: { }curly-brackets for square-bracketssteps: [] - add
.asStepafter each step - add comma (,) after each step, except the last
The syntax is not so elegant, but the diagnostics are better.
Validate Output of Publishers
letoperation=assert( publisher: myPublisher, eventuallyReceives:"🙉","🙊","🙈", andCompletes:false)somethingHappensAndPublisherReceives("🙉")somethingHappensAndPublisherReceives("🙊")somethingHappensAndPublisherReceives("🙈")operation.wait(0.0001)
Validate Output and Successful Completion of Publishers
letoperation=assert( publisher: myPublisher, eventuallyReceives:"🙉","🙊","🙈", andCompletesWith:.isSuccess)somethingHappensAndPublisherReceives("🙉")somethingHappensAndPublisherReceives("🙊")somethingHappensAndPublisherReceives("🙈")somethingHappensAndPublisherCompletes()operation.wait(0.0001)
Validate Output and some Failure of Publishers
letoperation=assert( publisher: myPublisher, eventuallyReceives:"🙉","🙊","🙈", andCompletesWith:.isFailure)somethingHappensAndPublisherReceives("🙉")somethingHappensAndPublisherReceives("🙊")somethingHappensAndPublisherReceives("🙈")somethingHappensAndPublisherFails(SomeError())operation.wait(0.0001)
Validate Output and specific Failure of Publishers
letoperation=assert( publisher: myPublisher, eventuallyReceives:"🙉","🙊","🙈", andCompletesWith:.failedWithError{ errorin error==SomeError("123")})somethingHappensAndPublisherReceives("🙉")somethingHappensAndPublisherReceives("🙊")somethingHappensAndPublisherReceives("🙈")somethingHappensAndPublisherFails(SomeError("123"))operation.wait(0.0001)
Validate No Output but completion of Publishers
letoperation=assert( publisher: myPublisher, completesWithoutValues:.isSuccess)somethingHappensAndPublisherCompletes()operation.wait(0.0001)
About
Testing helpers and extensions for SwiftRex
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.