我刚刚开始在我的Rails应用程序上进行一些测试,其中我使用了design进行身份验证。我刚刚生成了rspec,只添加了
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
到我的规格/支持/设计。rb文件。我还将Capybara添加到gemfile中的测试组中,运行bundle命令,等等。但是当我在测试中访问时,我得到了一个no method错误。下面是我的user_spec.rb:
的一个例子require 'spec_helper'
describe "Club" do
before(:each) do
@club ||= FactoryGirl.create(:club)
FactoryGirl.create(:membership)
User.all.each{|x| x.delete}
end
describe "privacy" do
it "shouldn't be shown any tournament if the user is private" do
attributes = FactoryGirl.attributes_for(:user)
user = User.create!({private: true}.merge(attributes))
assert(user.private)
visit "/user/#{user.id}/tournaments"
page.should have_no_selector(".tournament-list")
end
end
end
当我运行rspec时,出现如下错误:
Failures:
1) Club privacy shouldn't be shown any tournament if the user is private
Failure/Error: visit "/user/#{user.id}/tournaments"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x007ff0ea113108>
# ./spec/user_spec.rb:14:in `block (3 levels) in <top (required)>'
对于capybara规格,我使用:
describe "Club", :type => :feature, :js => true do
include Capybara::DSL
it "..."
end
而不是User.all。每个{|x| x.d eet},使用DatabaseCleaner
您可以轻松地将Capybara::DSL添加到RSpec config spec_helper中。rb
RSpec.configure do |config|
.....
.......
config.include(Capybara::DSL)
end