Apex 错误:“保存错误:方法不存在或签名不正确”

发布于 2024-12-04 12:08:13 字数 947 浏览 5 评论 0原文

我目前正在学习 apex(使用 Force.com IDE),在为自定义控制器编写测试时遇到了一些麻烦。

控制器类如下:

public with sharing class CustomController {
    private List<TestObject__c> objects;

    public CustomController() {
        objects = [SELECT id, name FROM TestObject__c];
    }

    public List<TestObject__c> getObjects() {
        return objects;
    }
}

测试类为:

@isTest
private class ControllerTest {
    static testMethod void customControllerTest() {
        CustomController controller = new CustomController();

        System.assertNotEquals(controller, null);

        List<TestObject__c> objects;

        objects = controller.getObjects();

        System.assertNotEquals(objects, null);    
    }
}

objects =controller.getObjects(); 行上,我收到一条错误消息:

保存错误:方法不存在或签名不正确:[CustomController].getObjects()

有人知道我为什么会收到此错误吗?

I'm currently learning apex (using the Force.com IDE), and I'm running into some trouble when writing a test for a custom controller.

The controller class is as follows:

public with sharing class CustomController {
    private List<TestObject__c> objects;

    public CustomController() {
        objects = [SELECT id, name FROM TestObject__c];
    }

    public List<TestObject__c> getObjects() {
        return objects;
    }
}

and the test class is:

@isTest
private class ControllerTest {
    static testMethod void customControllerTest() {
        CustomController controller = new CustomController();

        System.assertNotEquals(controller, null);

        List<TestObject__c> objects;

        objects = controller.getObjects();

        System.assertNotEquals(objects, null);    
    }
}

On the objects = controller.getObjects(); line I'm getting an error which says:

Save error: Method does not exist or incorrect signature: [CustomController].getObjects()

Anyone have an idea as to why I'm getting this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-12-11 12:08:13

一个很好的简写:

public List<TestObject__c> objects {get; private set;}

它为你创建了 getter/setter,并且在我看来看起来更干净。至于您当前的问题,是的 - 很难将代码直接保存到生产中 - 特别是在单独的文件中测试类。

最好在沙箱/开发组织中执行此操作,然后部署到生产环境(部署到服务器 - Force.com IDE)。但如果您必须直接保存到生产中,那么我会将测试方法与您的类结合起来。但从长远来看,在专用测试类之上使用 @test 是正确的选择。这样就不会消耗您宝贵的资源。

A nice shorthand:

public List<TestObject__c> objects {get; private set;}

It creates the getter/setter for you and looks cleaner imo. As for your current issue, yes - it's hard saving code directly into production - especially with test classes in separate files.

Best to do this in a sandbox/dev org then deploy to production (deploy to server - Force.com IDE). But if you must save directly into production then I'd combine test methods with your class. But in the long run, having @test atop a dedicated test class is the way to go. It won't consume your valuable resources this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文