本文介绍了在Salesforce中如何进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了有关Salesforce的代码编写工作,并且要发布单元测试,必须覆盖至少 75%.

I've done writing code on salesforce and in order to release the unit tests have to cover at least 75%.

我所面对的是,从classTwo调用方法的classOne也必须覆盖classOne 中的classTwo单元测试,即使它是在classTwo文件中完成的已经.

What I am facing is that the classOne that calls methods from classTwo also have to cover classTwo's unit test within classOne even though it is done in classTwo file already.

文件MyClassTwo

 public with sharing class ClassTwo {

    public String method1() {
        return 'one';
    }

    public String method2() {
        return 'two';
    }

    public static testMethod void testMethod1() {

        ClassTwo two = new ClassTwo();
        String out = two.method1();
        system.assertEquals(out, 'one'); //valid
    }

    public static testMethod void testMethod2() {
        ClassTwo two = new ClassTwo();
        String out = two.method2();
        system.assertEquals(out, 'two'); // valid
    }

}

文件MyClassOne

 public with sharing class ClassOne {

    public String callClassTwo() {
        ClassTwo foo = new ClassTwo();
        String something = foo.method1();

        return something;
    }

    public static testMethod void testCallClassTwo() {
        ClassOne one = new ClassOne();
        String out = one.callClassTwo();

        system.assertEquals(out, 'one');
    }
}

测试MyClassOne的结果不会返回100%的测试覆盖率,因为它说我没有覆盖MyClassOne文件内部的MyClassTwo method2()部分.

The result of testing MyClassOne would not return 100% test coverage because it says I have not covered MyClassTwo method2() part inside of MyClassOne file.

但是您已经看到,我已经在MyClassTwo文件中为MyClassTwo编写了单元测试.

But I already wrote unit test for MyClassTwo inside of MyClassTwo file as you can see.

那么这是否意味着我必须将MyClassTwo文件中的单元测试复制并粘贴到MyClassOne上?

So does this mean I have to copy and paste the unit test in MyClassTwo file over to MyClassOne?

这样做可以使我获得100%的覆盖率,但这看起来确实很烦人和荒谬.在ClassA和ClassB中有相同的测试.我做错了还是这样?

Doing so gives me 100% coverage but this seems really annoying and rediculous. Having same test in ClassA and ClassB....? Am I doing wrong or is this the way?

话虽如此,是否可以在salesforce中创建模拟对象?我还没弄清楚怎么做..

Having said, is it possible to create mock object in salesforce? I haven't figure how yet..

http://sites.force.com/answers/ideaView?c=09a30000000D9xt&id=087300000007m3fAAA&returnUrl=/apex/ideaList%3Fc%3D09a30000000D9xt%26category% 3DApex%2B%2526%2BVisualforce%26p%3D19%26sort%3Dpopular

UDPATE

我重新编写了代码并在上面进行了更新,这次可以确定classOne测试不会返回100%,即使它没有调用classTwo method2()

I re-wrote the code and updated above, this time for sure classOne test would not return 100% even though it is not calling classTwo method2()

推荐答案

在Salesforce世界中,有关Java模拟库的评论不是很有帮助;)在我的项目中,我们通常旨在在测试方法中创建自己的测试数据,真正的功能,检查结果...以及Salesforce方面的整个测试框架负责事务回滚(因此,无论测试失败还是通过,最终都不会将测试数据保存到DB).

Comments about Java mock libraries aren't very helpful in Salesforce world ;) At my projects we usually aimed for making our own test data in the test method, calling real functionality, checking the results... and whole test framework on Salesforce side is responsible for transaction rollback (so no test data is saved to DB in the end regardless whether the test failed or passed).

反正...

Masato,您的类无法编译(方法超出类范围,public String hello(),没有返回任何字符串)...修复问题后,我右键单击MyClassA-> Force.com->运行测试,并充满了代码覆盖范围没有任何问题,因此您的问题必须放在其他地方...

Masato, your classes do not compile (methods outside class scope, public String hello() without any String returned)... After I fixed it I simply right-clicked the MyClassA -> Force.com -> run tests and got full code coverage without any problems so your issue must lie somewhere else...

外观如下: http://dl.dropbox.com/u/709568/stackoverflow/masato_code_coverage.png

我试图考虑可能出了什么问题...您确定所有类都已编译并保存在服务器端吗?您是否将测试方法放在与功能相同的类中或放在单独的类中(通常我将单独的类名设为类似MyClassATest之类的名称).如果是单独的类-您在哪个文件上单击运行测试"?最后但并非最不重要的一点-如果在从沙箱到生产的部署过程中遇到此问题,请确保已在部署向导中选择了所需的所有类?

I'm trying to think what might have gone wrong... are you sure all classes compile and were saved on server side? Did you put test methods in same classes as functionality or in separate ones (generally I make separate class name with similar name like MyClassATest). If it's a separate class - on which file did you click "run tests"?Last but not least - if you're facing this issue during deployment from sandbox to production, make sure you selected all classes you need in the deployment wizard?

这篇关于在Salesforce中如何进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:23