我在SalesForce Apex代码中看到一些我不理解的行为。它似乎打破了代码安全规则。我有一个看起来像这样的控制器:
public with sharing class CaseController {
//Properties
public Case TheCase { get; private set; }
//
//Constructor
public CaseController(ApexPages.StandardController controller) {
//Some unimportant stuff
}
//
//Validates all data coming in from the view and saves the case
public PageReference Save() {
//Some other unimportant stuff
}
}
看起来像这样的测试:
private static testMethod void Save_WithCompleteCase_SavesCase()
{
//Given
User user = GetTestUser('Standard User');
Product2 theProduct = GetTestProduct();
Case theCase = GetTestCase(user, theProduct);
System.runAs(user) {
//When
CaseController controller = new CaseController(new ApexPages.StandardController(theCase));
controller.TheCase.Subject = 'Test Case'; //Making a change to test it saved
PageReference page = controller.Save();
//Then
}
}
请注意,我的控制器在“TheCase”上有一个私有的setter,但我可以在测试类中更改它的值。此代码适用于SalesForce并由其处理。为什么?是否有一些特殊的测试类允许他们访问其他类的私有成员?
谢谢!
答案 0 :(得分:1)
将Case属性设置为私有集只能保护控制器免于从其下面更改Case。
对不起!!