0

Please help me fix my test.

 @isTest
public class TestProductTableApplication {
    @testSetup  static void setup() {
    ProductTable__c product = new ProductTable__c (Name = 'carrot', Price__c = 10, Amount__c = 1500 );
    INSERT product;
}
  @isTest static void getProducts (){
   ProductTable__c testproduct = ProductTableCmpController.getProducts();
   System.assertEquals(testproduct.Price__c, 10);
}  
}

Method does not exist or incorrect signature: void getProducts() from the type ProductTableCmpController

public class ProductTableCmpController {
@AuraEnabled
public static List < ProductTable__c > getProducts (String searchKeyWord) {
    String searchKey = '%'+ searchKeyWord + '%';
    List <ProductTable__c> returnList = new List <ProductTable__c> ();
    List <ProductTable__c> lstOfProducts = [select id, Name, Amount__c, Price__c, ProductType__c, ReleaseDate__c, Available__c, AddedDate__c from ProductTable__c where Name LIKE: searchKey ORDER BY AddedDate__c DESC];
    for (ProductTable__c acc: lstOfProducts) {
        returnList.add(acc);
    }
    return returnList;
} 

2 Answers 2

2

Welcome to SF Stack Exchange.

First you need to set your instantiation ProductTable__c testproduct = ProductTableCmpController.getProducts(); in your test to a List. The method you are calling is a list, so the test method needs to also be a list.

Your method also receive a parameter of type string.

public static List < ProductTable__c > getProducts (String searchKeyWord)

So you need to pass also a string to your test method. Something like this:

List<ProductTable__c> testproduct = ProductTableCmpController.getProducts(serachString);

It is telling you that it doesn't exist because you don't have a method in that controller that don't accept parameters, which it is true. But you have the one accepting a string as parameter which it is the correct method you are using.

Something like this should work:

@isTest
   public class TestProductTableApplication {
    @testSetup  static void setup() {
    ProductTable__c product = new ProductTable__c (Name = 'carrot', 
    Price__c = 10, Amount__c = 1500 );
    INSERT product;
    }
    @isTest static void getProducts (){
    String searchString = 'Whatever';
    List<ProductTable__c> testproduct = ProductTableCmpController.getProducts(searchString);
    // put your asserts here
   }  
  }

FOR DELETE:

@AuraEnabled
public static void deleteProductTable (List<Id> productIds) {
    List<ProductTable__c> productsToDelete = [SELECT Id FROM 
ProductTable__c WHERE Id in :productIds];
    delete productsToDelete;       
}

The method deleteProductTable accepts a List of Ids List productIds

That means that you need to pass the exact data type to you test method in order to make it work.

So you can do something like this:

@isTest static void deleteProductTable (){
 List<Id> productTableIds = new List<Id>();

 // Lest just add two Ids to keep this simple.
 ProductTable__c pTableOne = new ProductTable__c();
 ProductTable__c pTableTwo = new ProductTable__c();

 // You need to add those mandatory fields before inserting a record
 // In this case we use the Name field as the only mandatory field
 // but you need to figurate out the ones you need for your 
 // ProductTable__c record
 pTableOne.Name = 'POne';
 insert pTableOne;

 pTableTwo.Name = 'PTwo';
 insert pTableTwo;

 // After inserting the records we have access to their Ids
 // Now we add the ids to our list
 productTableIds.add(pTableOne.Id);
 productTableIds.add(pTableOne.Id);

 // Now we can pass our list to the method as parameter

 List<ProductTable__c> testproduct = 
 ProductTableCmpController.deleteProductTable(productTableIds);
// put your asserts here

}

4
  • Thank you so much.
    – yuri
    Commented Oct 6, 2019 at 20:48
  • but what if the method accepts a parameter of type list?
    – yuri
    Commented Oct 6, 2019 at 21:18
  • Then you pass a list. Something like this: public static List < Whatever > getProducts (List<Whatever> listOfWhatever) in your test you need to build a List and pass it to the test method. @isTest static void getWhatever (){ List<Whatever> myList = new List<Whatever>() // You can add single record to the list by using myList.add(whatever); //Then pass the list to the test method List<Whatever> testproduct = ProductTableCmpController.getProducts(myList); } Commented Oct 7, 2019 at 11:38
  • Thank you again.
    – yuri
    Commented Oct 7, 2019 at 13:00
0
    @isTest
public class TestProductTableApplication {
        @testSetup  static void setup() {
        ProductTable__c product = new ProductTable__c (Name = 'carrot', Price__c = 10, Amount__c = 1500 );
        INSERT product;
    }
   @isTest static void deleteProductTable(){
        ProductTable__c product = new ProductTable__c(Name = 'Test');
        insert product;
       ProductTable__c testproduct = ProductTableCmpController.deleteProductTable(product.Id);
        Integer productsAfter = [select count() from ProductTable__c];
        System.assertEquals(1, productsAfter);
    }
}

Method does not exist or incorrect signature: void deleteProductTable(Id) from the type ProductTableCmpController

@AuraEnabled
    public static void deleteProductTable (List<Id> productIds) {
        List<ProductTable__c> productsToDelete = [SELECT Id FROM ProductTable__c WHERE Id in :productIds];
        delete productsToDelete;       
    }
4
  • Welcome to SFSE yuri. Can you call out what you chaged, and why? As it is while this may fix the issue, it doesn't really teach or explain why.
    – Matt Lacey
    Commented Oct 7, 2019 at 2:45
  • In my test, the method getProducts did not receive parameters, so it did not exist. We passed the text value into it and thanks to this the method worked.
    – yuri
    Commented Oct 7, 2019 at 7:03
  • But how to repeat this for a method deleteProductTable.
    – yuri
    Commented Oct 7, 2019 at 7:06
  • Check the DELETE section on the first answer. Commented Oct 7, 2019 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .