Comments (2)
@seanhenry I have a feature idea that solves this and it'd likely be less effort than directly adding an expectation or custom templates.
Problem Description
The problem I'm running into is that I have a ViewModel I'd like to test. Inside the ViewModel
there's a function which performs an asynchronous request.
protocol ViewModelDelegate: AnyObject {
func didFetch(data: Data)
}
class ViewModel {
let network: Network
var delegate: ViewModelDelegate?
func performNetworkRequest() {
network.shared.performRequest({ [delegate] data in
delegate?.didFetch(data: data)
})
}
}
In my test, I'd like to wait until the delegate is called to make an assertion.
class ViewModelTest: XCTestCase {
var network: MockNetwork!
var delegate: MockViewModelDelegate!
var viewModel: ViewModel!
override func setUp() {
/*..setup..*/
}
func test_performNetworkRequest() {
let testData = Data()
network.stubbedPerformRequestResult = testData
viewModel.performNetworkRequest()
// I want to block here
XCTAssertEqual(delegate.invokedDidFetchParameters.data, testData)
}
}
Solution/Feature Idea
In addition to generating the invoked/stubbed members, we generate a "perform" member which is a closure accepting the same parameters as the method.
class MockViewModelDelegate: ViewModelDelegate {
var performDidFetch: ((data: Data) -> Void)?
func didFetch(data: Data) {
performDidFetch?(data)
}
}
This would allow for the caller to set behaviour to occur after the method is called. This could even be the use of an expectation like in Jimmy's case.
func test_performNetworkRequest() {
let testData = Data()
network.stubbedPerformRequestResult = testData
let exp = expectation()
delegate.performDidFetch = { data in
XCTAssertEqual(data, testData)
exp.fulfill()
}
viewModel.performNetworkRequest()
waitForExpectations(timeout: 1.0)
}
This feature is similar to Perform
in SwiftyMocky.
https://github.com/MakeAWishFoundation/SwiftyMocky#4-take-action-when-a-stubbed-method-is-called---perform
from swiftmockgeneratorforxcode.
Hi Jimmy,
Thanks for raising this issue and I'm pleased you're finding this tool useful. The templates I'm using are trying to follow the test doubles described in this article and I would like to keep the generated code as simple as possible so that it could be handwritten if required.
However, I think this trick might help. Create an intermediate mock which is generated by this tool. Then extend this class with your mock containing your expectation.
protocol MyProtocol {
func someFunctionThatsCalledInAnimationBlock()
}
class _MyMock: MyProtocol {
var invokedSomeFunctionThatsCalledInAnimationBlock = false
var invokedSomeFunctionThatsCalledInAnimationBlockCount = 0
var someFunctionThatsCalledInAnimationBlockExpectation: XCTestExpectation?
func someFunctionThatsCalledInAnimationBlock() {
invokedSomeFunctionThatsCalledInAnimationBlock = true
invokedSomeFunctionThatsCalledInAnimationBlockCount += 1
someFunctionThatsCalledInAnimationBlockExpectation?.fulfill()
}
}
final class MyMock: _MyMock {
var someFunctionThatsCalledInAnimationBlockExpectation: XCTestExpectation?
override func someFunctionThatsCalledInAnimationBlock() {
super. someFunctionThatsCalledInAnimationBlock()
someFunctionThatsCalledInAnimationBlockExpectation?.fulfill()
}
}
Whilst this will prevent you from adding the expectation after the mock is regenerated, you will still have to manually write the initial subclass. I have suggested a feature previously which was to enable custom templates. So you could copy an existing template and add your modifications as you wish. If that would solve your problem then we could change this feature request to implement that feature instead? And if it gets enough support then I will build it.
from swiftmockgeneratorforxcode.
Related Issues (20)
- Installation? HOT 1
- Using this with Objective C HOT 1
- Typo in permission error message
- Generated code does not follow the enclosing class access control HOT 1
- Extension doesn't work on Big Sur HOT 16
- ObjectToTest ???? HOT 2
- Can't find Mock Generator on Big Sur HOT 1
- import AST errors HOT 2
- Stubs are not generated when the member variable end in a '$'
- Optional properties have a force-unwrapped stub
- Installation via Homebrew
- Stopped working under Big Sur and Xcode 12.4 HOT 1
- Extensions not supported HOT 1
- Indentation follows Xcode ctrl-i
- Naming Conventions
- Generate async mocks HOT 6
- Cannot compile because of missing private framework `SwiftToolkit`
- Optional function parameter break generator
- Handle generic completion handlers in mocked function
- Can I enter it in the function?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftmockgeneratorforxcode.