gpt4 book ai didi

ruby-on-rails - 这是一个很好的书面测试还是很丑陋?

转载 作者:行者123 更新时间:2023-11-28 20:52:05 26 4
gpt4 key购买 nike

describe "Team::Membership" do
let(:team) { Team.last }
let(:user) { User.last }
let(:membership) {user.apply_for(team)}

it "can be rejected" do
assert_difference "team.denied_applications.reload.count", +1 do
assert_difference "team.applications.reload.count", -1 do
assert_difference "team.members.reload.count", 0 do
membership.reject!
end
end
end
membership.state.must_equal "denied"
end

如果申请被拒绝,范围denied_applications增加1,applications为-1,members完全不变.

# team.rb
has_many :memberships, dependent: :destroy
has_many :members, -> { where(team_memberships: {state: :confirmed}) }, through: :memberships, source: :user
has_many :ex_members, -> { where(team_memberships: {state: :left}) }, through: :memberships, source: :user
has_many :applications, -> { where(state: :pending) }, class_name: "Membership"
has_many :denied_applications, -> { where(state: :denied) }, class_name: "Membership"

所以我的问题更有可能是:这是一个很好的写作测试吗还是你会直接写作

team.denied_applications.reload.count.must_equal(1)
team.applications.reload.count.must_equal(0)
team.members.reload.count.must_equal(0)

最佳答案

有些人会争辩说差异断言应该在不同的测试中,但恕我直言,在那种情况下这不是问题。

在我看来不是最优的是你在调用

let(:team) { Team.last }
let(:user) { User.last }

为确保您的测试不相互依赖或相互干扰,应在测试运行之间清理数据库,因此如果这样做,您将编写如下内容:

let(:team) { Team.create }
let(:user) { User.create }

或者您可以使用类似 factory_girl 的东西来更轻松地创建记录,例如,如果您正在进行一些验证并且不想在所有地方输入有效数据。

关于ruby-on-rails - 这是一个很好的书面测试还是很丑陋?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687971/

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