我有两个模型文章和书签。两者都被定义为acts_as_paranoid 对象。他们之间的关系是
文章.rb
has_many :bookmarks, foreign_key: 'article_doi', primary_key: 'doi', dependent: :destroy
Run Code Online (Sandbox Code Playgroud)
书签.rb
belongs_to :article, foreign_key: 'article_doi', primary_key: 'doi'
Run Code Online (Sandbox Code Playgroud)
现在我想真正删除一个书签对象并删除相关的书签对象。
@article.destroy!
Run Code Online (Sandbox Code Playgroud)
并检查
@article.bookmarks
Run Code Online (Sandbox Code Playgroud)
没有删除文章或其书签。我怎样才能真正从数据库及其关联的书签中删除它们?
我正在学习RSpec。我只想在单个文件上运行rspec。我通过完成它
cd to/my/project
Run Code Online (Sandbox Code Playgroud)
和
rspec ./spec/parent/child/spec.rb
Run Code Online (Sandbox Code Playgroud)
但是我下达命令后
rspec --order rand:seed
Run Code Online (Sandbox Code Playgroud)
我的rspec从头开始运行所有测试。如何撤消该命令?
我是 RSpec 的新手。我的模型 user_profile.rb 中有一个方法
def self.create_from_supplement(device, structure)
xml = Nokogiri.parse(structure.to_s)
user_profile = nil
auth_type = xml.%('auth_supplement/auth_type').inner_html
if 'user' == auth_type
user_details_str = xml.%('auth_supplement/supplement_data/content').inner_html rescue nil
return nil if user_details_str.blank?
user_details_xml = Nokogiri.parse(user_details_str)
user_name = user_details_xml.%('username').inner_html
user_profile = UserProfile.find_or_initialize_by(name: user_name)
if user_profile.save
device.update_attributes(user_profile_id: user_profile.id)
else
raise "User Profile Creation Failed because of #{user_profile.errors.full_messages}"
end
end
return user_profile
end
Run Code Online (Sandbox Code Playgroud)
我正在编写一个单元测试用例来测试当 user_profile.save 失败时,测试用例将期望引发异常。但在我的 user_profiles 表中我只有一列:名称。
user_profile.save失败时如何测试? 这里最重要的问题是我找不到任何方法使这个 user_profile.save 失败。
有些人建议使用 RSpec 存根。我们该怎么做呢?