You need to sign in to do that
Don't have an account?
Apply Unit of Work Principles in Apex - Test Passing, but Still Error When Submitting
Hi All,
I receive the following error when attempting to check the challenge for Apply Unit of Work
Challenge Not yet complete... here's what's wrong:
The 'challangeComplete' method in the 'UnitOfWorkTest' class has not successfully passed all tests. Ensure that you run the tests and it passes successfully before attempting this challenge again.
However, when I run my code in Developer Console, my test passes. Any ideas on the problem? Here is the code:
@isTest
public class UnitOfWorkTest {
@isTest static void challengeComplete(){
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
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'+i + ' ' +j);
uow.registerNew(c, Contact.AccountId, a);
Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
uow.registerRelationship(n, Note.ParentId, a);
uow.registerNew(n, Note.ParentId, a);
}
}
uow.commitWork();
fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
for (Account a : [SELECT Id, Name, (SELECT Id, LastName FROM Contacts), (SELECT Id, ParentId, Title, Body FROM Notes) FROM Account]) {
a.Name = 'Test';
uow2.registerDirty(a);
Integer i = 0;
for (Contact c : a.Contacts) {
c.LastName = 'Test';
uow2.registerDirty(c);
a.Notes[i].Body='Test';
uow2.registerDirty(a.Notes[i]);
i++;
}
}
test.startTest();
uow2.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());
}
}
I receive the following error when attempting to check the challenge for Apply Unit of Work
Challenge Not yet complete... here's what's wrong:
The 'challangeComplete' method in the 'UnitOfWorkTest' class has not successfully passed all tests. Ensure that you run the tests and it passes successfully before attempting this challenge again.
However, when I run my code in Developer Console, my test passes. Any ideas on the problem? Here is the code:
@isTest
public class UnitOfWorkTest {
@isTest static void challengeComplete(){
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
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'+i + ' ' +j);
uow.registerNew(c, Contact.AccountId, a);
Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
uow.registerRelationship(n, Note.ParentId, a);
uow.registerNew(n, Note.ParentId, a);
}
}
uow.commitWork();
fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
for (Account a : [SELECT Id, Name, (SELECT Id, LastName FROM Contacts), (SELECT Id, ParentId, Title, Body FROM Notes) FROM Account]) {
a.Name = 'Test';
uow2.registerDirty(a);
Integer i = 0;
for (Contact c : a.Contacts) {
c.LastName = 'Test';
uow2.registerDirty(c);
a.Notes[i].Body='Test';
uow2.registerDirty(a.Notes[i]);
i++;
}
}
test.startTest();
uow2.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());
}
}
All Answers
Please consider below points.
1.Please check if you have connected to same DE org where you have done your work in the trailhead.To do this click on" launch your hands on org" and select the DE org or trailhead playground where you have your work and then check challenge.
OR
Go to Trailhead Profile -- settings -- make the DE org which you have worked as default then check the challenge.
Trailhead released with new updates. Please refer below link how to take challenges in trailhead.
https://force.desk.com/customer/portal/articles/2643793-trailhead-profile-signup-login-faq?b_id=13478
Hope this helps you!
If this helps you please mark it as solved.
Thanks and Regards
Sandhya
thanks for your response. Unfortunately, this doesn't resolve the issue. I still get the error. I re-wrote the test class and still get the same error.
My test class passes when executed in Developer Console, still. However, the same error message persists.
This is the revised code
@isTest
public class UnitOfWorkTest {
@isTest static void challengeComplete(){
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
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'+i + ' ' +j);
uow.registerNew(c, Contact.AccountId, a);
Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
//uow.registerRelationship(n, Note.ParentId, a);
//uow.registerNew(n, Note.ParentId, a);
uow.registerNew(n, Note.ParentId, c);
}
}
uow.commitWork();
fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
Id oldAccountId;
Account a2;
for (Contact c : [SELECT Id, LastName, AccountId, Account.Name, (SELECT Id, ParentId, Title, Body FROM Notes) FROM Contact Order By AccountId, Id]) {
if (oldAccountId != c.AccountId) {
oldAccountId = c.AccountId;
a2 = new Account(Id=c.AccountId, Name='Test');
uow2.registerDirty(a2);
}
c.LastName = 'Test';
uow2.registerDirty(c);
c.Notes[0].Body = 'Test';
uow2.registerDirty(c.Notes[0]);
}
test.startTest();
uow2.commitWork();
//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());
}
}
Why do you need a second uow?
I've passed challenge with this code:
I didn't even used uow.registerRelationship. What for? Note has only one relationship and it's with acc. so uow.registerNew(n, Note.ParentId, acc) will take care of that. If Note would have another relationship, it would make sense to use registerRelationship but this is simple example so why complicate things.
(I just wasn't sure that if I have to create all these records or if there is some class/method for this challenge which would do it for me :-D )
@Francis Crump
Well I didn't paste it all.
You need to wrapp it inside @isTest public UnitOfWorkTest {
[myCode]
}
I thought it would be clear. I paste there only method.
Realy awesome code WOrked effienciently.Thanks a lot.
System.LimitException: Too many SOQL queries: 101
How can I overcome this ?
My code:
@isTest
public with sharing class UnitOfWorkTest {
public UnitOfWorkTest() {}
@isTest
static void challengeComplete(){
// Create a Unit Of Work
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[] {
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType});
// Do some work!
for(Integer o=0; o<100; o++) {
Account acc = new Account();
acc.Name = 'UoW Test Name ' + o;
uow.registerNew(acc);
for(Integer i=0; i<5; i++) {
Contact con = new Contact();
con.firstName = 'test' + i;
con.lastName = 'tester' + i;
uow.registerNew(con, Contact.accountId, acc);
Note aNote = new Note();
aNote.title = 'Test Note'+ i;
aNote.body = 'Some body Note' + i;
uow.registerNew(aNote, Note.parentId, acc);
}
}
// Commit the work to the database!
uow.commitWork();
System.assertEquals(100, [Select Id from Account].size());
System.assertEquals(500, [Select Id from Contact].size());
System.assertEquals(500, [Select Id from Note].size());
}
}
Error: System.AssertException: Assertion Failed: Expected: 500, Actual: 600
Note: I've tried to change the j=0 to j=1 and the assertion error is "expected=500 actual=400"
1.Deploy the ApexMocks open source library.
2. Deploy the Apex Common open source library.
3. Run all the test class in your org
4. Create below UnitOfWorkTest
5. Run this calss: UnitOfWorkTest
6. Pass the challenge.
@isTest
public class UnitOfWorkTest {
@isTest static void challengeComplete(){
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
for (Integer i=0 ; i<100 ; i++) {
Account a = new Account(Name= 'Test' Account + i);
uow.registerNew(a);
for (Integer j=0 ; j<5 ; j++) {
Contact c = new Contact(LastName = 'Test Contant '+i + ' ' +j);
uow.registerNew(c, Contact.AccountId, a);
Note n = new Note(Body='Test '+i + '' + j, Title='Test Notes'+i+j);
//uow.registerRelationship(n, Note.ParentId, a);
//uow.registerNew(n, Note.ParentId, a);
uow.registerNew(n, Note.ParentId, c);
}
}
uow.commitWork();
fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
new Schema.SObjectType[]{
Account.SObjectType,
Contact.SObjectType,
Note.SObjectType
}
);
Id oldAccountId;
Account a2;
for (Contact c : [SELECT Id, LastName, AccountId, Account.Name, (SELECT Id, ParentId, Title, Body FROM Notes) FROM Contact Order By AccountId, Id]) {
if (oldAccountId != c.AccountId) {
oldAccountId = c.AccountId;
a2 = new Account(Id=c.AccountId, Name='Test');
uow2.registerDirty(a2);
}
c.LastName = 'Test';
uow2.registerDirty(c);
c.Notes[0].Body = 'Test';
uow2.registerDirty(c.Notes[0]);
}
test.startTest();
uow2.commitWork();
//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());
}
}
I have followed the below steps and it worked for me :