1
我在SalesForce Apex代碼中看到一些行爲,我不明白。它似乎破壞了代碼安全規則。我有一個控制器,它看起來是這樣的: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處理。爲什麼?對於允許他們訪問其他類的私有成員的測試類,有什麼特別的嗎?
謝謝!
補充一下 - 我看到在我的RSS源,並認爲我會得到一個簡單的10分;-) – metadaddy 2011-03-17 03:24:59