Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Proper Tests LifeCycle #677

Merged
merged 3 commits into from
Jul 11, 2023

Conversation

michalsrutek
Copy link
Contributor

Use Proper Tests LifeCycle

XCTestCase works differently from a standard Swift class. It uses Objective-C runtime in the background.

That is why there should never be stored properties created directly inside of the class.

The reason being, that with having

final class PhoneNumberTextFieldTests: XCTestCase {
    private let phoneNumberKit = PhoneNumberKit()
    ...
}

the test case lifecycle for n tests would look like this

  • init phoneNumberKit-1
  • init phoneNumberKit-2
  • ...
  • init phoneNumberKit-n
  • run test-1
  • run test-2
  • ...
  • run test-n

(Note that the phoneNumberKit-s never get deinitialized! ⚠️ )

There are (at least) 2 ways to solve this

  1. Create phoneNumberKit at the start of each test (this adds visual noise)
  2. Define phoneNumberKit as an unwrapped optional and use setUp() to initialize it and tearDown() to clear it

I would suggest we go with the second approach.

That way, we would have

  • init phoneNumberKit-1
  • run test-1
  • deinit phoneNumberKit-1 ✅ // deinit was not called before (at all!)
  • init phoneNumberKit-2 ✅ // all the init-s were run before the tests
  • run test-2
  • deinit phoneNumberKit-2
  • ...

Source: iOS Unit Testing by Example by Jon Reid or his website (or better yet - you can try it yourself!)

Copy link
Collaborator

@bguidolim bguidolim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this refactoring :)

@bguidolim bguidolim merged commit d98d549 into marmelroy:master Jul 11, 2023
2 checks passed
cgrindel-self-hosted-renovate bot added a commit to cgrindel/rules_swift_package_manager that referenced this pull request Aug 16, 2023
….7" (#515)

This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
|
[marmelroy/PhoneNumberKit](https://togithub.com/marmelroy/PhoneNumberKit)
| patch | `from: "3.6.6"` -> `from: "3.6.7"` |

---

### Release Notes

<details>
<summary>marmelroy/PhoneNumberKit (marmelroy/PhoneNumberKit)</summary>

###
[`v3.6.7`](https://togithub.com/marmelroy/PhoneNumberKit/releases/tag/3.6.7)

[Compare
Source](https://togithub.com/marmelroy/PhoneNumberKit/compare/3.6.6...3.6.7)

#### What's Changed

- Use Proper Tests LifeCycle by
[@&#8203;michalsrutek](https://togithub.com/michalsrutek) in
[marmelroy/PhoneNumberKit#677
- Updated metadata to version metadata/8.13.17 by
[@&#8203;github-actions](https://togithub.com/github-actions) in
[marmelroy/PhoneNumberKit#678
- Updated metadata to version metadata/8.13.18 by
[@&#8203;github-actions](https://togithub.com/github-actions) in
[marmelroy/PhoneNumberKit#680

**Full Changelog**:
marmelroy/PhoneNumberKit@3.6.6...3.6.7

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNC4yIiwidXBkYXRlZEluVmVyIjoiMzYuMjQuMiIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: Self-hosted Renovate Bot <361546+cgrindel-self-hosted-renovate[bot]@users.noreply.github.enterprise.com>
MahranBayed added a commit to MahranBayed/PhoneNumberKit that referenced this pull request Aug 28, 2023
…into upgrade

* 'master' of https://github.com/marmelroy/PhoneNumberKit: (125 commits)
  Updated metadata to version metadata/8.13.19 (marmelroy#685)
  Release 3.6.7
  Updated metadata to version metadata/8.13.18 (marmelroy#680)
  Updated metadata to version metadata/8.13.17 (marmelroy#678)
  Use Proper Tests LifeCycle (marmelroy#677)
  Release 3.6.6
  Added configuration to keep the search bar always visible for the default country picker (marmelroy#676)
  Updated metadata to version metadata/8.13.16 (marmelroy#675)
  Improve PhoneNumberKitParsingTests (marmelroy#674)
  Remove unnecessary iOS 11 checks (marmelroy#673)
  Release 3.6.5
  Fix marmelroy#670 - Infinite loop on clearButtonRect (marmelroy#671)
  Release 3.6.4
  Add support for optional text field insets applied only if a new initilizer is used (marmelroy#669)
  Update README.md (marmelroy#666)
  Updated bug report template
  Stop using NSObject (fix marmelroy#634) (marmelroy#665)
  Release 3.6.3
  Release 3.6.2
  Added tests to PR (marmelroy#664)
  ...

# Conflicts:
#	PhoneNumberKit/PhoneNumberKit.swift
#	PhoneNumberKit/Resources/PhoneNumberMetadata.json
#	PhoneNumberKit/UI/CountryCodePickerViewController.swift
#	PhoneNumberKit/UI/PhoneNumberTextField.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants