Writing tests for the JournalEntry class
As you have seen, the initializer for the JournalEntry class will only return a JournalEntry instance if title and body are not empty, and rating is between 0 and 5 inclusive. If these conditions are not met, the initializer will return nil. To test this, you will write tests to confirm that a valid JournalEntry instance is created when the above conditions are met, and nil is returned when they are not. Follow these steps:
- Click the JRNLTests file inside the JRNLTests folder in the Project navigator.
- Modify the contents of this file as follows:
import Testing @testable import JRNL struct JRNLTests { @Test func JournalEntryInitializationSucceeds() { let zeroRatingJournalEntry = JournalEntry(rating: 0, title: "Zero", body: "Zero rating entry") #expect(zeroRatingJournalEntry != nil) let positiveRatingJournalEntry = JournalEntry( rating: 5, title: "Highest"...