所以我在Rails应用程序中有了这个特性规范。
require 'rails_helper'
feature 'As a signed in user' do
let(:user) {create(:user)}
let(:article) { create(:article)}
before {login_as(user, :scope => :user )}
scenario 'I can edit article with valid attributes' do
visit edit_article_path(article)
puts current_path
fill_in 'article_name', with: 'Valid name'
fill_in 'article_content', with: 'Valid content'
# save_and_open_page
click_button 'Update Article'
expect(article.name).to eq 'Valid name'
end
endfill_in实际上没有用Valid name和Valid content填充输入字段。我通过保存和打开页面来调试它,并且值保持不变,所以我想这不是我的Rails应用程序的问题,而是Capybara的问题。在其他未来的规范中:
require 'rails_helper'
feature 'As a signed in user' do
let(:user) {create(:user)}
let(:article) { build(:article)}
before {login_as(user, :scope => :user )}
scenario 'I can create article with valid attributes' do
visit '/articles/new'
fill_in 'Name', with: article.name
fill_in 'article_content', with: article.content
expect {click_button 'Create Article'}.to change {Article.count}.by(1)
end
end一切都如期而至。我得到的错误是:
Failures:
1) As a signed in user I can edit article with valid attributes
Failure/Error: expect(article.name).to eq 'Valid name'
expected: "Valid name"
got: "Name1"
(compared using ==)
# ./spec/features/articles/article_update_spec.rb:15:in `block (2 levels) in <top (required)>'它的原因是什么,以及如何通过规范?
发布于 2016-05-25 18:05:46
看来您使用的是机架测试驱动程序,因为您的测试没有标记为js: true。假设这是真的,您的问题是article已经在内存中,并且在检查名称更改之前没有从DB重新加载。将您的测试更新为以下内容将强制重新加载,然后您的测试将通过
expect(article.reload.name).to eq 'Valid name'如果您不使用机架测试驱动程序,那么click_button将是异步的,而且您还会遇到其他问题,因为在检查数据库对象更改之前,您没有检查浏览器中的更改。
https://stackoverflow.com/questions/37441468
复制相似问题