所以我的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
end
和fill_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)>'
它可能是什么原因以及如何制定规范?
答案 0 :(得分:1)
您似乎正在使用机架测试驱动程序,因为您的测试未被标记为js:true。假设这是真的,您的问题是article
已经在内存中,并且在检查名称更改之前不会从数据库重新加载。将测试更新为以下内容将强制重新加载,然后您的测试应该通过
expect(article.reload.name).to eq 'Valid name'
如果您没有使用机架测试驱动程序,那么click_button将是异步的,您将遇到其他问题,因为您在检查数据库对象更改之前未检查浏览器中的更改。