我在 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”上有一个私有设置器,但我可以在测试类中更改它的值。此代码有效并由 SalesForce 处理。为什么?允许他们访问其他类的私有成员的测试类有什么特别之处吗?
谢谢!!