Chuyển đến nội dung
Skillpedia
Kiểm thử15k · #10990

principle-test-behavior-not-implementation

Skill này làm gì

Cung cấp cho agent một quy tắc viết và giữ test tốt. Dành cho lập trình viên có test bị hỏng ở mọi lần refactor, hoặc vẫn pass khi code sai.

Khi nào nên dùng

  • ✓Khi bạn viết, thay đổi hoặc giữ một test.
  • ✓Khi test thất bại ở mỗi lần refactor dù hành vi không đổi.
  • ✓Khi một test vẫn pass sau khi bạn cố ý phá vỡ hành vi.

Liên kết skill

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-test-behavior-not-implementation/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-test-behavior-not-implementation/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Test của chúng tôi bị hỏng mỗi lần refactor, dù không có gì thay đổi với người dùng. Hãy viết lại chúng để kiểm tra hành vi.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent kiểm tra từng test theo hai chỉnh sửa tưởng tượng: một chỉnh sửa phá vỡ hành vi, mà test phải bắt được, và một chỉnh sửa chỉ thay đổi phần nội bộ, mà test phải sống sót. Nó viết lại hoặc xóa các test thất bại ở một trong hai kiểm tra, và điều khiển code qua giao diện công khai.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Test Behavior, Not Implementation

A test is a claim about what the code does for its users: a caller, an end user, or another service. Behavior is what those users can observe through the interface they use. That includes return values, errors, state they can read back, and effects on systems outside the code, such as a request sent, a row written, or a message emitted. Implementation is what they cannot observe: private helpers, internal call order, intermediate data, and which internal collaborator did the work.

Judge each test against two imagined edits:

  • Break the behavior. Make the code return a wrong value, skip an effect, or do the effect twice. If the test still passes, it proves nothing. The usual causes are an assertion looser than the contract, an expected value computed by the code under test, a restated constant or prompt string that never runs the code that reads it, and an assertion on data that the test built itself.
  • Change the internals. Keep every observable result the same and restructure the code underneath. If the test now fails, it is pinned to the implementation. It will need a rewrite on every refactor and still catches no bug.

Keep a test that catches the first edit and survives the second. Rewrite or delete the rest. To write a test that does both, drive the code through its users' interface with a concrete input. Derive the expected value from the requirement, not from a run of the code. Assert what the contract promises, no less and no more.

The contract decides, not the shape of the assertion. When "an empty cart never charges" is the promise, an assertion that the payment client got no call is behavior. Pair it with a case where the charge happens, so that the test shows it can see a call. The payload that a mocked external service receives is behavior, because that service observes it. A call to an internal helper is implementation, because no user can see it. An empty list is the exact answer when no record matches, and a hollow check when the test only needs some result. An exact error message pins wording that no caller reads. The error type that callers branch on is the contract.

A test can pin an invariant instead of a call, such as a key that must exist in two tables or a compile-time type check. The invariant is the behavior.

Câu hỏi thường gặp

Hành vi là gì?

Những gì người dùng code có thể quan sát, như giá trị trả về, lỗi, trạng thái đọc được và các hiệu ứng như một tin nhắn được gửi đi.

Test yếu là gì?

Test mà giá trị kỳ vọng do chính code đang test tính ra, hoặc khẳng định lỏng hơn hợp đồng.

Test có nên kiểm tra các hàm nội bộ không?

Không. Hàm nội bộ và thứ tự gọi nội bộ là chi tiết cài đặt, không phải hành vi.

Chi tiết

Mô tả gốc của tác giả

Apply when you write, change, or keep a test. Assert what the code's users can observe, so that the test fails when that behavior breaks and keeps passing when only the internals change.

repo: cursor/plugins

Độ phổ biến

Sao GitHub
15k
Vị trí xếp hạng
#10990
Giấy phép
—
Cập nhật
31/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự