In my controller I have a List
:
public List<ts2__Job__c> uaVacancies {
get;
set;
}
And a simple function to query:
public PageReference getUAVacancies() {
uaVacancies = [SELECT Id, Name, ts2__Account__c, ts2__Contact__c, ts2__Priority__c, ts2__Stage__c, ts2__Job_Number__c, CreatedDate FROM ts2__Job__c WHERE (ts2__Recruiter__c = :user OR Development_Owner__c = :user) AND ts2__Status__c = 'Open'];
return null;
}
In my test class' @testSetup
I'm creating a ts2__Job___c
object, then I have a method that calls the function getUAVacancies()
.
It all covers fine, but I wanted to assert that the list is populated with 1 record. So when I do:
System.assertEquals(1, uaVacancies.size());
The test fails saying I'm expecting 1, but getting 0.
I'm not sure what I'm doing wrong here! But here's my full test method:
@isTest static void testSearch() {
User u;
for (User i : [SELECT Id FROM User WHERE Email = '[email protected]' LIMIT 1]) {
u = i;
}
System.runAs(u) {
KN_VacanciesController con = new KN_VacanciesController();
con.getUAVacancies();
con.getPostedKNVacancies();
System.assertEquals(1, uaVacancies.size());
}
}
In the test record I'm inserting, I'm making sure that ts2__Status__c = 'Open'
and set the Development_Owner__c
to the user created in the test class.
Any reason why this is returning 0 when it should return 1?
Edit I changed the method as follows:
@isTest static void testSearch() {
User u;
for (User i : [SELECT Id FROM User WHERE Email = '[email protected]' LIMIT 1]) {
u = i;
}
ts2__Job__c j = new ts2__Job__c(
Name = 'Test Job',
ts2__Status__c = 'Open',
Development_Owner__c = u.Id
);
INSERT j;
System.runAs(u) {
KN_VacanciesController con = new KN_VacanciesController();
con.getUAVacancies();
con.getPostedKNVacancies();
System.assertEquals(1, con.uaVacancies.size());
}
}
And the test passed. What I don't understand is I created the exact same test__Job__c
object in @testSetup static void testData()
method. I checked the Sharing Settings of the object and they're Public Read/Write
and the Controller itself is public with sharing class
. I'm curious to know how come my test method can't access data in the @testSetup
?
@testSetup static void testData() {}
method.@testSetup
and trying to read it with useru
. If the OWD are set to private,u
won't be able to see that record.System.assertEquals(1, con.uaVacancies.size());
ts2__Job__c
record in thetestSearch()
method and it passed. The controller itself ispublic with sharing class
and the OWD for that object isPublic Read/Write
? Also yeah sorry I missed the con. off I didn't copy it straight from the code!