我正在使用Rspec 3.0和capybara 2.4.3。我在spec / features / named challenge_users_spec.rb
中制作了一个功能规范require "rails_helper"
feature "Challenge users" do
let!(:challenge) { create(:challenge)}
let(:user) { create(:user)}
context "as a registered user" do
before :each do
sign_in(user)
end
scenario "challenges himself" do
expect(current_path).to eq root_path
expect(page).to have_content challenge.name
click_button 'Accept'
expect(page).to have_content 'You accepted the challenge!'
expect(user.accepted_dares.count).to eq 1
end
scenario 'challenges other user' do
expect(current_path).to eq root_path
click_button 'Challenge others'
expect(current_path).to eq new_challenge_dare_path(challenge_id: challenge.id)
expect(page).to have_content 'Challenge with a bet!'
end
end
end
在第一种情况下,页面呈现正确的闪存消息但不更新用户。 在第二个场景中,点击“Challenge others”后,current_path设置为“/”而不是“challenge /:id / dares / new”,因此失败了最后两个期望。 Dares模型是一种嵌套路线:
resources :challenges do
resources :dares
end
控制器方法大多是标准的CRUD方法。
这是Rspec日志:
Failures:
1) Challenge users as a registered user challenges himself
Failure/Error: expect(user.accepted_dares.count).to eq 1
expected: 1
got: 0
(compared using ==)
# ./spec/features/challenge_users_spec.rb:18:in `block (3 levels) in <top (required)>'
2) Challenge users as a registered user challenges other user
Failure/Error: expect(current_path).to eq new_challenge_dare_path(challenge_id: challenge.id)
expected: "/challenges/2/dares/new"
got: "/"
(compared using ==)
# ./spec/features/challenge_users_spec.rb:26:in `block (3 levels) in <top (required)>'
Finished in 0.91199 seconds (files took 3.62 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./spec/features/challenge_users_spec.rb:10 # Challenge users as a registered user challenges himself
rspec ./spec/features/challenge_users_spec.rb:21 # Challenge users as a registered user challenges other user
型号:
敢
class Dare < ActiveRecord::Base
belongs_to :challenge, counter_cache: true
belongs_to :acceptor, class_name: "User"
belongs_to :challenger, class_name: "User"
has_many :votes
before_save :change_status
before_save :create_start_date
before_save :set_proof_array
def set_proof_array
if self.utube_link.nil?
self.utube_link = []
save!
end
end
def create_start_date
if status == 'Accepted' && start_date.blank?
self.start_date = DateTime.now
end
end
def change_status
if status.blank?
if self.acceptor_id == self.challenger_id
self.status = "Accepted"
else
self.status = "Pending"
end
end
end
...
end
用户
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :dares, foreign_key: :challenger_id
has_many :accepted_dares, class_name: "Dare", foreign_key: :acceptor_id
has_many :challenges, through: :dares
has_many :votes
validates :username, presence: true, uniqueness: true, length: { in: 2..50 }
....
end
挑战
class Challenge < ActiveRecord::Base
has_many :dares
has_many :users, through: :dares, source: :challenges
validates :name, presence: true, uniqueness: true, length: { in: 5..100 }
validates :description, presence: true, length: { in: 10..500 }
include PgSearch
pg_search_scope :search, against: [:name, :description],
using: {tsearch: {prefix: true, dictionary: "english" }}
...
end
修改
我没有提到这个,但我在我的应用程序中使用Turbolinks。 在我的测试中添加js:true后,第二个测试通过,但第一个测试仍然会抛出错误:
1) Challenge users as a registered user challenges himself
Failure/Error: expect(user.reload.accepted_dares.count).to eq 1
expected: 1
got: 0
(compared using ==)
答案 0 :(得分:5)
这是两个无关的问题。
用户模型未更新的原因是它在内存中与后端更改的实例完全独立。您需要强制它从数据库重新加载:
expect(user.reload.accepted_dares.count).to eq 1
current_path
未给出预期值的原因是click_button
是异步操作。在新页面有机会加载之前,点击按钮后会立即执行current_path
上的断言。
Capybara的许多操作(例如find
)会自动等待并重试,直到条件成立为止(在find
的情况下,它等待选择器匹配页面上的某些内容),但是因为current_path
没有任何条件,它会立即返回当前值(Capybara不知道您在调用该方法时所期望的当前路径)。
更好的方法是在单击按钮并重新加载页面后,查找一些您希望仅在页面上的内容。这迫使Capybara在继续之前等待这些内容。
在这种情况下,颠倒你所拥有的两个断言的顺序应该可以解决这个问题:
expect(page).to have_content 'Challenge with a bet!'
expect(current_path).to eq new_challenge_dare_path(challenge_id: challenge.id)