我有一个Rails 5应用,正在测试模型中的回调。我想确定回调已被调用,但是我不希望在调用API并发送电子邮件时实际触发以下行:
Emp emp = new Emp();
Dept dept = new Dept();
Class<?> cls = emp.getClass().getSuperclass(); // it is Class<Object>
Class<?> cls = dept.getClass().getSuperclass(); // it is Class<Emp>
我的测试如下:
class Emp {
private String firstName = "first_name";
private String lastName = "last_name";
}
class Dept extends Emp {
private String dpFirstName = "dp_first_name";
private String dpLastName = "dp_last_name";
}
public static void getSuperClassFields(Dept dept) throws IllegalAccessException {
Class<?> cls = dept.getClass().getSuperclass();
for (Field field : cls.getDeclaredFields()) {
field.setAccessible(true);
System.out.println(field.getName() + " = " + field.get(dept));
}
}
public static void main(String[] args) throws IllegalAccessException {
getSuperClassFields(new Dept());
// in console you can see
// firstName = first_name
// lastName = last_name
}
这可行,但是上面提到的行被解雇了。如何在不触发对response = bird.request(body, employee.location.birdeye)
的调用的情况下测试此回调?这是我的模型:
it 'expects to send request through birdeye if valid' do
req = build(:review_request)
expect(req).to receive(:send_request)
req.save
end
答案 0 :(得分:1)
如果您只想检查回调是否已被调用,那么我认为模拟send_request
可能是可行的方法。
尝试关注
before do
allow_any_instance_of(ReviewRequest).to receive(:send_request).and_return(true)
end
it 'expects to call :send request after creating a ReviewRequest' do
allow_any_instance_of(ReviewRequest).to receive(:send_request)
create(:review_request)
end
如果您要测试send_request
的实现,则将存根Birdeye