所以我对rspec很新,我过去和Capybara一起使用过Cucumber,但是我试图用Capybara而不是Cucumber(我不需要Cucumber中的BDD小黄瓜语言)更多地转向ACTUAL rspec
我现在的文件夹结构是spec / test_helper和spec / features / google_test.rb(现在只是一个示例)
My gemfile has included:
gem 'capybara'
gem 'poltergeist'
gem 'selenium-webdriver'
gem 'rpsec'
我的test_helper.rb文件(在project / spec文件夹中)
#test_helper.rb
#Load up Capybara
require 'rspec'
require 'capybara/rspec'
require 'capybara'
require 'capybara/dsl'
#Load up Poltergeist
require 'capybara/poltergeist'
#Set JS Supported Driver
Capybara.javascript_driver = :poltergeist
我的google_test.rb(在规格/功能中)
require 'test_helper'
Capybara.current_driver = :selenium
Capybara.run_server = false
Capybara.app_host = 'www.google.com'
describe "Visit Google Home Page", :type => feature do
it 'Google' do
visit ('/')
end
end
正在运行rspec spec/features/google_test.rb
:
故障:
1)访问Google主页Google 失败/错误:访问('/') NoMethodError: '
中的未定义方法visit' for #<RSpec::ExampleGroups::VisitGoogleHomePage:0x007f8ef546ad30> # ./spec/features/google_test.rb:9:in
块(2级)以0.00044秒结束(文件加载0.47304秒)1 例如,1次失败
失败的例子:
rspec ./spec/features/google_test.rb:8#访问Google主页Google
有什么想法吗?
答案 0 :(得分:1)
当你需要capybara / rspec时,它配置RSpec以将capybara DSL包含在类型:feature的测试中。有几种方法可以在RSpec测试中设置类型
describe "xyz", :type => :feature do # note :feature is a symbol
# test goes here
end
feature "xyz" do # alias that automatically sets the type
# tests go here
end
或通过配置RSpec来根据目录名称设置类型 - https://www.relishapp.com/rspec/rspec-rails/docs/directory-structure
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
确保您已使用其中一种方法,然后visit
应该可用