gpt4 book ai didi

ruby-on-rails - 如何干掉 Rails 的 RSpec 测试

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:39 25 4
gpt4 key购买 nike

我正在学习 RSpec,但我注意到我的代码中有很多重复项。以下只是许多其他例子中的两个例子。有没有一种方法可以创建共享测试而不必遍历每个单独的属性?

  describe "validation" do
describe "user_id" do
it "should not be blank or nil" do
@comment.user_id = nil
@comment.should_not be_valid
@comment.user_id = " "
@comment.should_not be_valid
end

it "should an integer" do
@comment.user_id = "a"
@comment.should_not be_valid
end
end

describe "post_id" do
it "should not be blank or nil" do
@comment.post_id = nil
@comment.should_not be_valid
@comment.post_id = " "
@comment.should_not be_valid
end

it "should an integer" do
@comment.post_id = "a"
@comment.should_not be_valid
end
end
end

最佳答案

你可以试试这个:https://github.com/thoughtbot/shoulda-matchers

它为您的模型提供了许多简单的匹配器

关于ruby-on-rails - 如何干掉 Rails 的 RSpec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15773874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com