Last active
December 16, 2021 20:36
-
-
Save alexliesenfeld/3ef2e494225988f6bbeca02316b52129 to your computer and use it in GitHub Desktop.
[BLOG] main.rs test | Mocking HTTP Services in Rust with httpmock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(test)] | |
mod tests { | |
use crate::GithubClient; | |
use httpmock::MockServer; | |
use serde_json::json; | |
#[test] | |
fn create_repo_success_test() { | |
// Arrange | |
let server = MockServer::start(); | |
let mock = server.mock(|when, then| { | |
when.method("POST") | |
.path("/user/repos") | |
.header("Authorization", "token TOKEN") | |
.header("Content-Type", "application/json"); | |
then.status(201) | |
.json_body(json!({ "html_url": "http://example.com" })); | |
}); | |
let client = GithubClient::new("TOKEN", &server.base_url()); | |
// Act | |
let result = client.create_repo("myRepo"); | |
// Assert | |
mock.assert(); | |
assert_eq!(result.is_ok(), true); | |
assert_eq!(result.unwrap(), "http://example.com"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment