今回もなぜかハマる・・・
Write a Test Method using the Unit of Work
The test class must be named UnitOfWorkTest with a test method named challengeComplete. Call the commitWork method to update the database. Run the test at least once before attempting the challenge. It must pass successfully with the following assertions:
https://trailhead.salesforce.com/ja/content/learn/modules/apex_patterns_sl/apex_patterns_sl_apply_uow_principles
- System.assertEquals(100, [Select Id from Account].size());
- System.assertEquals(500, [Select Id from Contact].size());
- System.assertEquals(500, [Select Id from Note].size());
最初作ったのがこのソースコード。
@isTest
public class UnitOfWorkTest {
@isTest static void challengeComplete() {
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[] { Account.SObjectType, Contact.SObjectType, Note.SObjectType }
);
// Create 100 Account records
List<Account> accounts = new List<Account>();
for(Integer i = 0; i < 100; i++) {
Account acc = new Account(Name = 'Test Account ' + i);
accounts.add(acc);
}
uow.registerNew(accounts);
// Commit the Account records to the database
uow.commitWork();
// Reload the Unit of Work
uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[] { Account.SObjectType, Contact.SObjectType, Note.SObjectType }
);
// Create 500 Contact records
List<Contact> contacts = new List<Contact>();
for(Integer i = 0; i < 500; i++) {
Contact con = new Contact(LastName = 'Test Contact ' + i, AccountId = accounts[i % 100].Id);
contacts.add(con);
}
uow.registerNew(contacts);
// Create 500 Note records
List<Note> notes = new List<Note>();
for(Integer i = 0; i < 500; i++) {
Note n = new Note(Title = 'Test Note ' + i, Body = 'Test Note Body ' + i, ParentId = accounts[i % 100].Id);
notes.add(n);
}
uow.registerNew(notes);
// Commit the unit of work
uow.commitWork();
// Assertions
System.assertEquals(100, [Select Id from Account].size());
System.assertEquals(500, [Select Id from Contact].size());
System.assertEquals(500, [Select Id from Note].size());
}
}
これだとTestは成功するのに、チャレンジの判定をすると、なぜか「Could not find the assertion for 100 account records in the ‘challengeComplete’ method.」と言われてアサーションが認識されず。
いろいろ調べまくって、見付けたのがコミュニティの以下のソース。
@IsTest
private class UnitOfWorkTest {
private static List<Schema.SObjectType> MY_SOBJECTS =
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
};
@IsTest static void challengeComplete() {
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS);
for (Integer i = 0; i < 100; i++) {
Account a = new Account(Name = 'Test' + i);
uow.registerNew(a);
for (Integer j = 0; j < 5; j++) {
Contact c = new Contact(LastName = 'Test' + String.fromCharArray(new List<Integer>{
65 + i
}));
uow.registerNew(c, Contact.AccountId, a);
Note n = new Note();
n.Body = 'Test' + String.fromCharArray(new List<Integer>{
65 + i
});
n.Title = 'Test' + String.fromCharArray(new List<Integer>{
65 + i
});
uow.registerRelationship(n, Note.ParentId, a);
uow.registerNew(n, Note.ParentId, a);
}
}
Test.startTest();
uow.commitWork();
Test.stopTest();
System.assertEquals(100, [SELECT Id FROM Account].size());
System.assertEquals(500, [SELECT Id FROM Contact].size());
System.assertEquals(500, [SELECT Id FROM Note].size());
}
}
引用:Apply Unit of Work Principles in Apex – Test Passing, but Still Error When Submitting
たぶん最初の「MY_SOBJECTS」の定義の所が大事なんだと思うんだけど・・・
なんでこれが入るとチャレンジが通るようになるのかよくわからず\(^o^)/
誰か教えて・・・!
コメント