Snapshot test with insta
  gJZEzXHb6nFn 2023年12月22日 39 0


Snapshot test with insta

(Jin Qing’s Column, Dec., 2023)

Insta crate is for unit test.
It asserts the value comparing to the snapshot value.
Usually the value is complex and the comparison is by the serialized string form.

Reference: https://insta.rs/docs/quickstart/

First add insta into the project:

cargo add insta --dev --features yaml

yaml is the suggested form of the snapshot.

Write a test case using assert_yaml_snapshot!():

fn split_words(s: &str) -> Vec<&str> {
    s.split_whitespace().collect()
}

#[test]
fn test_split_words() {
    let words = split_words("hello from the other side");
    insta::assert_yaml_snapshot!(words);
}

The first run of cargo test will fail and generate src/snapshots/test_insta__split_words.snap.new:

---
source: src/main.rs
assertion_line: 13
expression: words
---
- hello
- from
- the
- other
- side

Rename *.snap.new file into *.span, then further tests will compare the value with the snapshot file.

Or use cargo insta review to review the spanshot file.


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月22日 0

暂无评论

推荐阅读
  qn1eRyGNKz7T   2024年02月27日   159   0   0 Rust
  qn1eRyGNKz7T   2024年02月29日   114   0   0 Rust
  qn1eRyGNKz7T   2024年03月07日   107   0   0 Rust
  qn1eRyGNKz7T   2024年03月19日   174   0   0 Rust
  qn1eRyGNKz7T   2024年02月29日   104   0   0 Rust
  qn1eRyGNKz7T   2024年03月25日   115   0   0 Rust
  qn1eRyGNKz7T   2024年04月01日   238   0   0 Rust
  qn1eRyGNKz7T   2024年03月13日   145   0   0 Rust
  3Vc6H13Lg7Nk   2024年04月28日   50   0   0 Rust
  qn1eRyGNKz7T   2024年02月23日   119   0   0 Rust
gJZEzXHb6nFn