测试动作拒绝的内容
机器翻译
本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。
任务: 证明一个表单拒绝了它应该拒绝的内容——违反规则的字段、业务上不允许的值—— 并且在拒绝时什么都没有写入。
一个注册表单,有两种拒绝:q:param 的规则(在动作运行之前检查),以及动作本身中的 一条业务规则(这个地址已经是会员了):
yaml
# Recipe: testing what an action refuses — field rules and a business rule.
paths:
components: ./components
migrations: ./migrations
datasources:
db:
driver: sqlite
database: ./data/signup.dbsql
CREATE TABLE members (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
INSERT INTO members (name, email) VALUES ('Ana', 'ana@example.com');xml
<q:component name="Signup">
<q:action name="join" method="POST">
<q:param name="name" required="true" minlength="2" />
<q:param name="email" type="email" required="true" />
<q:query name="taken" datasource="db">
SELECT id FROM members WHERE email = :email
<q:param name="email" value="{email}" type="string" />
</q:query>
<q:if condition="taken_result.recordCount > 0">
<q:redirect url="/" flash="{email} is already a member." flashType="error" />
</q:if>
<q:query name="added" datasource="db">
INSERT INTO members (name, email) VALUES (:name, :email)
<q:param name="name" value="{name}" type="string" />
<q:param name="email" value="{email}" type="string" />
</q:query>
<q:redirect url="/" flash="Welcome, {name}!" />
</q:action>
<h1>Join</h1>
<q:if condition="flash"><p class="flash-{flashType}">{flash}</p></q:if>
<form method="POST" action="/?action=join">
<input name="name" />
<input name="email" type="email" />
<button>Join</button>
</form>
</q:component>error="name" 检查提交被拒绝的字段,message= 检查它旁边显示的文本。 由动作自己决定的拒绝是一次带提示消息的重定向,用 flash= 检查。 table= 配合 count= 证明什么都没有写入:
xml
<q:test name="a short name is refused on its field, with the rule's message" page="/">
<test:submit action="join" name="A" email="bia@example.com" />
<test:expect error="name" message="Must be at least 2 characters" />
<test:expect table="members" count="0" where="email = 'bia@example.com'" />
</q:test>
<q:test name="an address that is not an e-mail is refused on its field" page="/">
<test:submit action="join" name="Bia" email="bia-at-example" />
<test:expect error="email" />
<test:expect table="members" count="1" />
</q:test>
<q:test name="an address that is already a member is refused with a flash" page="/">
<test:submit action="join" name="Ana Again" email="ana@example.com" />
<test:expect redirect="/" flash="ana@example.com is already a member." />
<test:expect table="members" count="1" />
</q:test>
<q:test name="a new member is welcomed" page="/">
<test:submit action="join" name="Bia" email="bia@example.com" />
<test:expect redirect="/" flash="Welcome, Bia!" />
<test:expect table="members" count="1" where="email = 'bia@example.com'" />
</q:test>text
tests/signup.test.q
PASS a short name is refused on its field, with the rule's message
PASS an address that is not an e-mail is refused on its field
PASS an address that is already a member is refused with a flash
PASS a new member is welcomed
4 passed, 0 failedq:param 接受的规则(required、minlength、type="email"、enum……)见 Actions & Forms(英文)。
已测试: 本页导入了 examples/cookbook/testing/action-errors/ 中的文件, 上面的结果就是运行它们得到的报告。