所有确认订阅者都会收到新刊
让我们再写一个集成测试, 这次针对理想情况的子集: 如果我们有一个确认的订阅者, 他们会收到一封包含新一期新闻通讯的电子邮件。
编写测试助手
与上一个测试一样,我们需要在执行测试逻辑之前将应用程序状态设置为我们想要的状态——它会调用另一个辅助函数,这次是为了创建一个已确认的订阅者。
通过稍微修改 create_unconfirmed_subscriber 函数,我们可以避免重复:
//! tests/api/newsletter.rs
// [...]
/// Use the public API of the application under test to create
/// an unconfirmed subscriber.
async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
let _mock_guard = Mock::given(path("/email"))
.and(method("POST"))
.respond_with(ResponseTemplate::new(200))
.named("Create unconfirmed subscriber")
.expect(1)
.mount_as_scoped(&app.email_server)
.await;
app.post_subscriptions(body.into())
.await
.error_for_status()
.unwrap();
// We now inspect the requests received by the mock Postmark server
// to retrieve the confirmation link and return it
let email_request = &app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
app.get_confirmation_links(email_request)
}
async fn create_confirmed_subscriber(app: &TestApp) {
// We can then reuse the same helper and just add
// an extra step to actually call the confirmation link!
let confirmation_link = create_unconfirmed_subscriber(app).await;
reqwest::get(confirmation_link.html)
.await
.unwrap()
.error_for_status()
.unwrap();
}
我们现有的测试无需任何更改, 并且可以在新测试中立即利用 create_confirmed_subscriber 函数:
//! tests/api/newsletter.rs
// [...]
#[tokio::test]
async fn newsletters_are_delivered_to_confirmed_subscribers() {
// Arrange
let app = spawn_app().await;
create_confirmed_subscriber(&app).await;
Mock::given(path("/email"))
.and(method("POST"))
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
// Act
let newsletter_request_body = serde_json::json!({
"title": "Newsletter title",
"content": {
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>",
}
});
let response = reqwest::Client::new()
.post(&format!("{}/newsletters", &app.address))
.json(&newsletter_request_body)
.send()
.await
.expect("Failed to execute request.");
// Assert
assert_eq!(response.status().as_u16(), 200);
// Mock verifies on Drop that we have sent the newsletter email
}
它失败了,正如它应该的那样:
Verifications failed:
- Mock #1.
Expected range of matching incoming requests: == 1
Number of matched incoming requests: 0