From e44da378c62f6fd63bfa02b68f023eb5381d1de2 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 4 Aug 2023 07:45:05 +0100 Subject: [PATCH] mock_fetch_as_text_with_rss_url takes a map of responses --- src/feed/find.rs | 9 +++++++-- src/test_utils.rs | 22 +++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/feed/find.rs b/src/feed/find.rs index dd6ec66..154c596 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -23,6 +23,8 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result { #[cfg(test)] mod tests { + use std::collections::HashMap; + use crate::test_utils::{ mock_fetch_as_text_with_rss_url, stub_network_download_as_mp3, stub_network_fetch_as_bytes, }; @@ -32,7 +34,10 @@ mod tests { fn finds_rss_url() -> Result<()> { //given let network_env = NetworkEnv { - fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"), + fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([( + "site@channel", + "the-rss-url", + )])), fetch_as_bytes: stub_network_fetch_as_bytes(), download_as_mp3: stub_network_download_as_mp3(), }; @@ -47,7 +52,7 @@ mod tests { fn error_if_channel_name_is_invalid() -> Result<()> { //given let network_env = NetworkEnv { - fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"), + fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([])), fetch_as_bytes: stub_network_fetch_as_bytes(), download_as_mp3: stub_network_download_as_mp3(), }; diff --git a/src/test_utils.rs b/src/test_utils.rs index 2008d08..95dd3b2 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -29,17 +29,21 @@ pub fn read_text_file(file_name: &str) -> Result> { .map(String::from) .collect()) } -pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn { - // TODO: returned function must return different values each time it is called - Box::new(move |_url: &str| { - Ok(format!( +pub fn mock_fetch_as_text_with_rss_url( + map: HashMap<&'static str, &'static str>, +) -> NetworkFetchAsTextFn { + Box::new(move |url: &str| match map.get(url) { + Some(url) => Ok(format!( r#" - - - - "#, + + + + "#, url - )) + )), + None => Err(Error::message( + format!("Unexpected request for {}", url).as_str(), + )), }) } pub fn mock_network_fetch_as_bytes_with_rss_entries(