gpt4 book ai didi

ruby-on-rails - Rspec,测试关联的存在

转载 作者:行者123 更新时间:2023-12-04 13:48:25 27 4
gpt4 key购买 nike

我正在尝试测试一个关联。

我有这两个类:

class Survey < ActiveRecord::Base
has_many :users, through: :users_surveys
has_many :users_surveys

def update_or_create_result(user_id, result, target)
user_survey = users_surveys.find_or_create_by(user_id: params[:user_id])
if survey_passed?(result, target)
user_survey.completed!
end
end
end

class User < ActiveRecord::Base
has_many :surveys, through: :users_surveys
has_many :users_surveys
end

class UsersSurvey < ActiveRecord::Base
belongs_to :user
belongs_to :survey
end

如果创建了调查和用户之间的关联,我如何使用 RSpec 进行测试?

最佳答案

只是想强调一下,RSpec 的当前 nex 语法 is the following :

describe User do
it { is_expected.to have_many(:surveys) } # shortcut for expect(subject).to
end

describe Survey do
it { is_expected.to belong_to(:user) }
end
should现在被认为是多年的旧语法:)

关于ruby-on-rails - Rspec,测试关联的存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34990316/

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