我正在开发一个在 ruby 1.9.3 下使用 rspec、devise 和 capybara 的 rails 3.2 应用程序
在使用 capybara 时,我在测试日志时遇到了很多问题,我的意思是在测试中使用 js: true metada 时。
fill_in
首先,在测试中,使用and方法通过 capybara 进行日志记录click_button
,当没有 时,它可以工作,因此正如js: true metadata
capybara wiki 所说login_as(user, :scope => :user)
。密码“当我确定它们都正确时(“请参阅下面的测试代码”)
并且还安装了 poltergeist gem,以使用 capybara 和 javascript 驱动程序加快速度,但我得到了与以前相同的结果,启用和禁用守望者。
这是我的测试
require "spec_helper"
describe "Expenses" do
context "given a logged user" do
let!(:user) { FactoryGirl.create(:confirmed_user) }
let!(:supermercado) { FactoryGirl.create(:category, user: user, name: 'Supermercado') }
let!(:books) { FactoryGirl.create(:category, user: user, name: 'Books') }
before(:each) do
login_as(user, :scope => :user)
end
context "given the user has categories defined" do
describe "creating expenses in bulk for today and yesterday" do
it "create every expense and redirects to index", :wip, :js do
visit new_expense_path
puts user.categories.inspect
puts user.inspect
puts books.inspect
within("#expenses_0_0") do
fill_in "expenses_0_0_name", with: "supermercado 1"
fill_in "expenses_0_0_amount", with: "6000"
select "Supermercado", from: "expenses_0_0_category_id"
fill_in "expenses_0_0_description", with: "cosas para la once"
end
click_button "Create Expenses"
page.should have_content("Expenses were successfully created")
current_path.should == expenses_path
end
end
end
end
end
但是现在,在守望者工作的情况下,我可以登录我的测试,但这是我的测试输出
~/code/personal_organizer(master…)% rspec -t wip . (ruby-1.9.3-p0@perorg)
Run options: include {:wip=>true}
[]
#<User id: 98, name: "foobar", email: "foobar1@example.com", encrypted_password: "$2a$04$L0ztIG4Lvn0Z1oqTOsGizOlH4soABWN7x0CY6TwNyA1o...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-02-27 01:11:20", last_sign_in_at: "2012-02-27 01:11:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-02-27 01:11:17", confirmation_sent_at: "2012-02-27 01:11:16", unconfirmed_email: nil, created_at: "2012-02-27 01:11:16", updated_at: "2012-02-27 01:11:20">
#<Category id: 209, name: "Books", color: "#fff", ctype: "expense", user_id: 98, created_at: "2012-02-27 01:11:18", updated_at: "2012-02-27 01:11:18">
F
Failures:
1) Expenses given a logged user given the user has categories defined creating expenses in bulk for today and yesterday create every expense and redirects to index
Failure/Error: select "Supermercado", from: "expenses_0_0_category_id"
Capybara::ElementNotFound:
cannot select option, no option with text 'Supermercado' in select box 'expenses_0_0_category_id'
# (eval):2:in `select'
# ./spec/requests/expenses_spec.rb:41:in `block (6 levels) in <top (required)>'
# ./spec/requests/expenses_spec.rb:38:in `block (5 levels) in <top (required)>'
这个错误对我来说有点莫名其妙......只需观察打印行的三个输出结果
puts user.categories.inspect
# []
puts user.inspect
# #<User id: 98, name: "foobar", email: "foobar1@example.com", encrypted_password: "$2a$04$L0ztIG4Lvn0Z1oqTOsGizOlH4soABWN7x0CY6TwNyA1o...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-02-27 01:11:20", last_sign_in_at: "2012-02-27 01:11:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-02-27 01:11:17", confirmation_sent_at: "2012-02-27 01:11:16", unconfirmed_email: nil, created_at: "2012-02-27 01:11:16", updated_at: "2012-02-27 01:11:20">
puts books.inspect
#<Category id: 209, name: "Books", color: "#fff", ctype: "expense", user_id: 98, created_at: "2012-02-27 01:11:18", updated_at: "2012-02-27 01:11:18">
正如我所说,我遇到了很多麻烦......并且开发模式下的应用程序工作得很好,这是我的测试库或配置的问题
顺便说一句......这是我的宝石版本
Using nokogiri (1.5.0)
Using ffi (1.0.11)
Using childprocess (0.3.1)
Using json_pure (1.6.5)
Using rubyzip (0.9.6.1)
Using selenium-webdriver (2.13.0)
Using xpath (0.1.4)
Using capybara (1.1.2)
Using warden (1.1.1)
Using devise (2.0.4)
Using factory_girl (2.6.0)
Using factory_girl_rails (1.7.0)
Using haml (3.1.4)
Using haml-rails (0.3.4)
Using launchy (2.0.5)
Using poltergeist (0.4.0)
Using pry (0.9.8.2)
Using yard (0.7.5)
Using pry-doc (0.4.1)
Using rails (3.2.1)
Using rspec-core (2.8.0)
Using rspec-expectations (2.8.0)
Using rspec-mocks (2.8.0)
Using rspec (2.8.0)
Using rspec-rails (2.8.1)
完整的项目可以在https://github.com/fespinoza/personal_organizer/tree/example中找到
也许是图书馆中的一个错误,并且可能是其中一个项目的拉取请求......我希望有人可以帮助我