Issue with Validate Model with snappshot test #1629
-
|
I'm trying to create a test like the default user test that's generated, called test_can_validate_model in tests/models/users.rs. It looks like this: #[tokio::test]
#[serial]
async fn test_can_validate_model() {
configure_insta!();
let boot = boot_test::<App>()
.await
.expect("Failed to boot test application");
let invalid_user = users::ActiveModel {
name: ActiveValue::set("1".to_string()),
email: ActiveValue::set("invalid-email".to_string()),
..Default::default()
};
let res = invalid_user.insert(&boot.app_context.db).await;
assert_debug_snapshot!(res);
}the insta shapshot for user looks like this: However, when I do it to my own model, I don't get this failed validation output. I tried to copy the test setup exactly, and implemented Validateable for ActiveModel: My code for the Organization model im trying to test: the test: #[tokio::test]
#[serial]
async fn test_can_validate_model() {
configure_insta!();
let boot = boot_test::<App>()
.await
.expect("Failed to boot test application");
let invalid_org = organizations::ActiveModel {
name: ActiveValue::set("1".to_string()),
contact_email: ActiveValue::set(Some("invalid-email".to_string())),
org_type: ActiveValue::Set("artist".to_string()),
..Default::default()
};
let res = invalid_org.insert(&boot.app_context.db).await;
assert_debug_snapshot!(res);
}the insta snapshot output: What am I doing wrong? Shouldn't the inserting of the model result in validation error? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hey @askor, It looks in |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, this solved it for me. All I was missing was adding self.validate()?;in |
Beta Was this translation helpful? Give feedback.
Hey @askor,
In the
usersmodel, we’ve implemented theValidatorlogic, and inbefore_save, the model calls thevalidatefunction. You can check the implementation here.It looks in
organizationsmodel doesn’t have this implemented yet. If you share your code, I can help you set it up.