rename stubs and implement mocks
This commit is contained in:
parent
d5b54a75d3
commit
2a3c6b5be0
2 changed files with 53 additions and 12 deletions
|
@ -24,7 +24,7 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
|||
mod tests {
|
||||
|
||||
use crate::test_utils::{
|
||||
mock_fetch_as_text_with_rss_url, stub_download_as_mp3, stub_fetch_as_bytes,
|
||||
mock_fetch_as_text_with_rss_url, stub_network_download_as_mp3, stub_network_fetch_as_bytes,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
@ -33,8 +33,8 @@ mod tests {
|
|||
//given
|
||||
let network_env = NetworkEnv {
|
||||
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
||||
fetch_as_bytes: stub_fetch_as_bytes(),
|
||||
download_as_mp3: stub_download_as_mp3(),
|
||||
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
||||
download_as_mp3: stub_network_download_as_mp3(),
|
||||
};
|
||||
//when
|
||||
let result = find("site", "@channel", &network_env)?;
|
||||
|
@ -48,8 +48,8 @@ mod tests {
|
|||
//given
|
||||
let network_env = NetworkEnv {
|
||||
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
||||
fetch_as_bytes: stub_fetch_as_bytes(),
|
||||
download_as_mp3: stub_download_as_mp3(),
|
||||
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
||||
download_as_mp3: stub_network_download_as_mp3(),
|
||||
};
|
||||
//when
|
||||
let result = find("site", "invalid-channel-name", &network_env);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{read_to_string, File},
|
||||
io::Write,
|
||||
str::from_utf8,
|
||||
|
@ -29,6 +30,7 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
|
|||
.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!(
|
||||
r#"
|
||||
|
@ -40,15 +42,54 @@ pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextF
|
|||
))
|
||||
})
|
||||
}
|
||||
pub fn stub_fetch_as_bytes() -> NetworkFetchAsBytesFn {
|
||||
Box::new(|_url: &str| Err(Error::message("Not implemented")))
|
||||
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
||||
feeds: HashMap<String, String>,
|
||||
) -> NetworkFetchAsBytesFn {
|
||||
Box::new(move |url| {
|
||||
if let Some(feed) = feeds.get(url).cloned() {
|
||||
Ok(bytes::Bytes::from(feed))
|
||||
} else {
|
||||
Err(Error::message(format!("No mock feed: {}", url).as_str()))
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn stub_download_as_mp3() -> NetworkDownloadAsMp3Fn {
|
||||
Box::new(|_url: &str| Err(Error::message("Not implemented")))
|
||||
pub fn mock_file_open(real_paths: Vec<String>) -> FileOpenFn {
|
||||
Box::new(move |path: &str| {
|
||||
if real_paths.contains(&path.to_string()) {
|
||||
Ok(File::open(path)?)
|
||||
} else {
|
||||
Err(Error::message(
|
||||
format!("Not implemented: file_open: {}", path).as_str(),
|
||||
))
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn stub_file_open() -> FileOpenFn {
|
||||
Box::new(|_path: &str| Err(Error::message("Not implemented")))
|
||||
|
||||
pub fn stub_network_fetch_as_bytes() -> NetworkFetchAsBytesFn {
|
||||
Box::new(|url: &str| {
|
||||
Err(Error::message(
|
||||
format!("Not implemented: network_fetch_as_bytes: {}", url).as_str(),
|
||||
))
|
||||
})
|
||||
}
|
||||
pub fn stub_network_download_as_mp3() -> NetworkDownloadAsMp3Fn {
|
||||
Box::new(|url: &str| {
|
||||
Err(Error::message(
|
||||
format!("Not implemented: network_download_as_mp3: {}", url).as_str(),
|
||||
))
|
||||
})
|
||||
}
|
||||
// pub fn stub_file_open() -> FileOpenFn {
|
||||
// Box::new(|path: &str| {
|
||||
// Err(Error::message(
|
||||
// format!("Not implemented: file_open: {}", path).as_str(),
|
||||
// ))
|
||||
// })
|
||||
// }
|
||||
pub fn stub_file_append_line() -> FileAppendLineFn {
|
||||
Box::new(|_: &str, _: &str| Err(Error::message("Not implemented")))
|
||||
Box::new(|path: &str, line: &str| {
|
||||
Err(Error::message(
|
||||
format!("Not implemented: file_append_line: {} to {}", line, path).as_str(),
|
||||
))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue