NgRx SignalStore 测试指南:从基础到高级实践
前言
在 Angular 状态管理领域,NgRx 一直是最受欢迎的解决方案之一。随着 Angular 16 引入 Signals 特性,NgRx 团队推出了 SignalStore 这一全新状态管理方案,它充分利用了 Signals 的响应式特性。本文将全面介绍如何为 SignalStore 编写有效的测试用例,涵盖从基础到高级的各种测试场景。
SignalStore 测试基础
无依赖测试
对于不依赖外部服务的简单 SignalStore,我们可以直接创建实例进行测试:
import { signalStore, withState } from '@ngrx/signals';
const CounterStore = signalStore(
withState({ count: 0 })
);
describe('CounterStore', () => {
it('should initialize with count 0', () => {
const store = new CounterStore();
expect(store.count()).toBe(0);
});
});
这种测试方式简单直接,适合验证 Store 的初始状态和基础功能。
使用 TestBed 测试
大多数情况下,SignalStore 会依赖其他服务,这时我们需要使用 Angular 的测试工具 TestBed:
import { TestBed } from '@angular/core/testing';
import { signalStore, withState, withMethods } from '@ngrx/signals';
const AuthStore = signalStore(
withState({ user: null }),
withMethods(({ $user }) => ({
login: (user) => patchState($user, user)
}))
);
describe('AuthStore', () => {
let store: AuthStore;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthStore]
});
store = TestBed.inject(AuthStore);
});
it('should update user on login', () => {
const testUser = { name: 'Test User' };
store.login(testUser);
expect(store.user()).toEqual(testUser);
});
});
依赖注入与模拟
模拟服务依赖
当 Store 依赖外部服务时,我们需要模拟这些服务:
import { inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
const DataStore = signalStore(
withState({ data: null }),
withMethods((store, http = inject(HttpClient)) => ({
loadData: () => {
http.get('/api/data').subscribe(data => {
patchState(store, { data });
});
}
}))
);
describe('DataStore', () => {
let store: DataStore;
let httpMock: jest.Mocked<HttpClient>;
beforeEach(() => {
httpMock = {
get: jest.fn()
} as any;
TestBed.configureTestingModule({
providers: [
DataStore,
{ provide: HttpClient, useValue: httpMock }
]
});
store = TestBed.inject(DataStore);
});
it('should load data from API', () => {
const testData = { id: 1 };
httpMock.get.mockReturnValue(of(testData));
store.loadData();
expect(httpMock.get).toHaveBeenCalledWith('/api/data');
expect(store.data()).toEqual(testData);
});
});
测试 rxMethod
rxMethod 是 SignalStore 中处理异步操作的重要特性,测试时需要特别注意:
import { rxMethod } from '@ngrx/signals';
import { of } from 'rxjs';
const SearchStore = signalStore(
withState({ results: [] }),
withMethods((store) => ({
search: rxMethod<string>((query$) => {
return query$.pipe(
switchMap(query =>
query ? http.get(`/search?q=${query}`) : of([])
),
tap(results => patchState(store, { results }))
);
}))
})
);
describe('SearchStore', () => {
let store: SearchStore;
let httpMock: jest.Mocked<HttpClient>;
beforeEach(() => {
httpMock = {
get: jest.fn()
} as any;
TestBed.configureTestingModule({
providers: [
SearchStore,
{ provide: HttpClient, useValue: httpMock }
]
});
store = TestBed.inject(SearchStore);
});
it('should perform search', () => {
const testResults = [{ id: 1 }];
httpMock.get.mockReturnValue(of(testResults));
store.search('test');
expect(httpMock.get).toHaveBeenCalledWith('/search?q=test');
expect(store.results()).toEqual(testResults);
});
it('should handle empty query', () => {
store.search('');
expect(store.results()).toEqual([]);
expect(httpMock.get).not.toHaveBeenCalled();
});
});
高级测试技巧
测试自定义 Store 特性
对于复杂的自定义 Store 特性,我们可以单独测试其行为:
function withLogger() {
return (store: SignalStore) => {
const actions = new Subject<string>();
return {
...store,
logAction: (action: string) => actions.next(action),
actions$: actions.asObservable()
};
};
}
describe('withLogger', () => {
it('should log actions', () => {
const TestStore = signalStore(withLogger());
const store = new TestStore();
const spy = jest.fn();
store.actions$.subscribe(spy);
store.logAction('test');
expect(spy).toHaveBeenCalledWith('test');
});
});
测试计算属性
计算属性(computed)是 SignalStore 的重要特性,测试时需要注意其惰性求值特性:
const CartStore = signalStore(
withState({ items: [] }),
withComputed(({ items }) => ({
total: computed(() =>
items().reduce((sum, item) => sum + item.price, 0)
)
}))
);
describe('CartStore', () => {
it('should calculate total', () => {
const store = new CartStore();
expect(store.total()).toBe(0);
patchState(store, {
items: [{ price: 10 }, { price: 20 }]
});
// 必须访问计算属性才会触发计算
expect(store.total()).toBe(30);
});
});
测试最佳实践
-
隔离测试:尽量将业务逻辑与状态管理分离,使得 Store 主要处理状态变更,业务逻辑由服务处理。
-
小范围测试:每个测试用例只验证一个特定行为,保持测试简洁明确。
-
状态验证:测试 Store 时,重点验证状态变更是否符合预期,而不是实现细节。
-
异步处理:对于涉及异步操作的测试,确保使用适当的工具(如 fakeAsync 或 waitForAsync)处理异步行为。
-
类型安全:充分利用 TypeScript 的类型系统,确保测试代码也能享受类型检查的好处。
结语
SignalStore 作为 NgRx 的新成员,为 Angular 应用状态管理带来了更简洁、更响应式的解决方案。通过本文介绍的各种测试方法,开发者可以确保 SignalStore 在各种场景下都能可靠工作。随着 SignalStore 的不断演进,测试方法也将持续完善,建议开发者关注 NgRx 官方文档获取最新测试实践。
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
请把这个活动推给顶尖程序员😎本次活动专为懂行的顶尖程序员量身打造,聚焦AtomGit首发开源模型的实际应用与深度测评,拒绝大众化浅层体验,邀请具备扎实技术功底、开源经验或模型测评能力的顶尖开发者,深度参与模型体验、性能测评,通过发布技术帖子、提交测评报告、上传实践项目成果等形式,挖掘模型核心价值,共建AtomGit开源模型生态,彰显顶尖程序员的技术洞察力与实践能力。00
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
MiniMax-M2.5MiniMax-M2.5开源模型,经数十万复杂环境强化训练,在代码生成、工具调用、办公自动化等经济价值任务中表现卓越。SWE-Bench Verified得分80.2%,Multi-SWE-Bench达51.3%,BrowseComp获76.3%。推理速度比M2.1快37%,与Claude Opus 4.6相当,每小时仅需0.3-1美元,成本仅为同类模型1/10-1/20,为智能应用开发提供高效经济选择。【此简介由AI生成】Python00
Qwen3.5Qwen3.5 昇腾 vLLM 部署教程。Qwen3.5 是 Qwen 系列最新的旗舰多模态模型,采用 MoE(混合专家)架构,在保持强大模型能力的同时显著降低了推理成本。00- RRing-2.5-1TRing-2.5-1T:全球首个基于混合线性注意力架构的开源万亿参数思考模型。Python00