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)
答案 0 :(得分:0)
有些人会争辩说差异断言应该在不同的测试中,但恕我直言,在这种情况下这不是问题。
在我看来,最重要的是你正在打电话
let(:team) { Team.last }
let(:user) { User.last }
为了确保您的测试不依赖于彼此或相互干扰,应该在测试运行之间清理数据库,因此如果这样做,您可以编写如下内容:
let(:team) { Team.create }
let(:user) { User.create }
或者您可以使用类似factory_girl
之类的内容来更轻松地创建记录,例如,如果您正在进行某些验证并且不想在任何地方输入有效数据。