go-study/chain/chain_test.go

34 lines
724 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package chain
import "testing"
func TestChain(t *testing.T) {
// 创建患者实例
patient := &Patient{name: "张三"}
// 创建看病处理者链
hospital := &HospitalChainHandler{}
// 设置病人看病的链路
processes := []PatientHandler{
&Reception{},
&Clinic{},
&Cashier{},
&Pharmacy{},
}
var current PatientHandler = hospital
for _, process := range processes {
current = current.SetNext(process)
}
// 执行处理链
if err := hospital.Execute(patient); err != nil {
t.Errorf("处理患者失败: %v", err)
}
// 检查最终状态
if patient.status != PatientStatusPharmacy {
t.Errorf("患者状态不正确,期望 %d实际 %d", PatientStatusPharmacy, patient.status)
}
}