function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sarthak das 4sarthak das 4 

I can't get the error "The 'RejectDoubleBooking' class did not achieve 100% code coverage via your test methods"

Any suggestion pls.Here is my code.


@isTest
private class TestRejectDoubleBooking{
    static testmethod void TestDoubleBooking() {
    Datetime now = System.now();
    Speaker__c speaker = new Speaker__c(First_Name__c='John', Last_Name__c='Smith');
    insert speaker;
    Session__c session1 = new Session__c(Name='Session 1', Session_Date__c=now);
    insert session1;
    Session__c session2 = new Session__c(Name='Session 2', Session_Date__c=now);
    insert session2;
    Session_Speaker__c assignment1 =
    new Session_Speaker__c(Session__c=session1.Id, Speaker__c=speaker.Id);
    insert assignment1;
    Session_Speaker__c assignment2 =
    new Session_Speaker__c(Session__c=session2.Id, Speaker__c=speaker.Id);
    Database.SaveResult result = Database.insert(assignment2, false);
    System.assert(!result.isSuccess());
}



}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sarthak,

Greetings to you!

You need to include both the methods in your test class - TestSingleBooking() and TestDoubleBooking(). Please try the below code, I have tested in my org and it is working fine.
 
@isTest
private class TestRejectDoubleBooking{
    
    static testmethod void TestSingleBooking() {
        Datetime now = System.now();
        Speaker__c speaker = new Speaker__c(First_Name__c='John', Last_Name__c='Smith');
        insert speaker;
        Session__c session = new Session__c(Name='Some Session', Session_Date__c=now);
        insert session;
        Session_Speaker__c assignment =
            new Session_Speaker__c(Session__c=session.Id, Speaker__c=speaker.Id);
        Database.SaveResult result = Database.insert(assignment, false);
        System.assert(result.isSuccess());
    }
    
    static testmethod void TestDoubleBooking() {
        Datetime now = System.now();
        Speaker__c speaker = new Speaker__c(First_Name__c='John', Last_Name__c='Smith');
        insert speaker;
        Session__c session1 = new Session__c(Name='Session 1', Session_Date__c=now);
        insert session1;
        Session__c session2 = new Session__c(Name='Session 2', Session_Date__c=now);
        insert session2;
        Session_Speaker__c assignment1 =
            new Session_Speaker__c(Session__c=session1.Id, Speaker__c=speaker.Id);
        insert assignment1;
        Session_Speaker__c assignment2 =
            new Session_Speaker__c(Session__c=session2.Id, Speaker__c=speaker.Id);
        Database.SaveResult result = Database.insert(assignment2, false);
        System.assert(!result.isSuccess());
    }  
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Volodymyr TsuprykVolodymyr Tsupryk
I had the same error verifying the task. I fixed the issue by deleting "ExampleTrigger" that was showing error while running test.