gpt4 book ai didi

ruby-on-rails - 失败的 rspec 测试应该通过

转载 作者:行者123 更新时间:2023-12-01 11:02:23 24 4
gpt4 key购买 nike

我有这个来自 michael hartl 书的测试

describe "follower/following counts" do
let(:user) { FactoryGirl.create(:user) }
let(:other_user) { FactoryGirl.create(:user) }

before do
sign_in(user)
visit root_path
user.follow!(other_user)
end
it { should have_link('0 following', href: following_user_path(user)) }
it { should have_link('1 follower', href: followers_user_path(user)) }
end

问题是这总是失败

it { should have_link('1 follower', href: followers_user_path(user)) }

但是当我把它改成

it { should have_link('0 follower', href: followers_user_path(user)) }

测试通过。这是我的模型代码:

用户.rb

has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :followers, through: :reverse_relationships, source: :follower

def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end

关系.rb

attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true

路线.rb

resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]

最佳答案

您需要在关注用户后访问该页面:

before do 
sign_in(user)
user.follow!(other_user)
visit root_path
end

否则页面将在用户被关注之前呈现,因此显示“0 关注者”。

关于ruby-on-rails - 失败的 rspec 测试应该通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9538726/

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