Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,27 @@ fn check_username(state: &AppState, string: &mut String, name: &str) {
async fn index() -> Html<&'static str> {
Html(std::include_str!("../chat.html"))
}

#[cfg(test)]
mod test {
use super::*;

#[tokio::test]
async fn test_check_username() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cool if the test reflected the behavior you're asserting in the test, maybe something like:
check_username_registers_unique_usernames

let user_set = Mutex::new(HashSet::new());
let (tx, _rx) = broadcast::channel(100);
let app_state = Arc::new(AppState { user_set, tx });
let mut username = String::new();
let name = "Ferris";

check_username(&app_state, &mut username, name);

assert!(app_state.user_set.lock().unwrap().contains(name));
assert_eq!(username, name);

username.clear();
check_username(&app_state, &mut username, name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also assert on app_state.user_set again, to ensure the HashSet was not changed


assert_eq!(username, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion could be simplified by doing the following

Suggested change
assert_eq!(username, "");
assert!(username.is_empty());

}
}
Loading