RSpec会在页面上抛出元素的误报

时间:2012-04-30 06:43:42

标签: ruby-on-rails ruby ruby-on-rails-3 rspec tdd

我正在尝试弄清楚我的应用程序目前发生了什么。我正在努力确保我可以在“编辑个人资料”页面上向用户显示不同的内容,具体取决于他们用于注册的方法(OAuth与否)。

我在浏览器中手动测试时只需将OAuth属性添加到用户模型并对其进行测试即可正常工作。

但是,在RSpec中我无法正确测试。实际上,无论我在网页上告诉它要查找哪些元素,它都会声称它们在那里。

describe "edit user" do

  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  describe "correct edit account options shown to normal users" do
    before do
      visit edit_user_registration_path(:user) 
    end

    it { should have_selector('h2',      text: 'Edit User') }
    it { should have_selector('input',   id:   'user_names')}
    it { should have_selector('input',   id:   'user_email', value: user.email) }
    it { should have_selector('input',   id:   'user_password') }
    it { should have_selector('input',   id:   'user_password_confirmation') }
    it { should have_selector('input',   id:   'user_current_password') }
    it { should have_selector('input',   type: 'submit') }
  end

  describe "correct edit account options shown to oauth users" do

    before do 
      user.update_attributes!(:oauth => true)
      visit edit_user_registration_path(:user)
    end

    it { should have_selector('h2',          text: 'Edit User') }
    it { should have_selector('input',       id:   'user_names')}
    it { should_not have_selector('input',   id:   'user_email') }
    it { should_not have_selector('input',   id:   'users_password') }
    it { should_not have_selector('input',   id:   'users_password_confirmation') }
    it { should_not have_selector('input',   id:   'user_current_password') }
    it { should have_selector('input',       type: 'submit') }
  end
end

我的RSpec集成测试的完整代码位于:https://gist.github.com/2556055

对此的任何帮助都将深表感谢。

1 个答案:

答案 0 :(得分:2)

幸运地从#rubyonrails的那些人手中解决了这个问题

原因是has_selector不带多个参数(因此为什么ID没有区别)。因此,应该使用have_css方法替换它们。

所以正确的实施方式是:

describe "edit user" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }

describe "correct edit account options shown to normal users" do
 before do
    visit edit_user_registration_path(:user) 
 end
 it { should have_selector('h2',      text: 'Edit User') }
 it { should have_css('input#user_name')}
 it { should have_css('input#user_email') }
 it { should have_css('input#user_password') }
 it { should have_css('input#user_password_confirmation') }
 it { should have_css('input#user_current_password') }
 it { should have_selector('input',   type: 'submit') }
end

describe "correct edit account options shown to oauth users" do

before do 
  user.update_attributes!(:oauth => true)
  visit edit_user_registration_path(:user)

end

  it { should have_selector('h2', text: 'Edit User') }
  it { should have_css('input#user_name')}
  it { should_not have_css('input#user_email') }
  it { should_not have_css('input#users_password') }
  it { should_not have_css('input#users_password_confirmation') }
  it { should_not have_css('input#user_current_password') }
  it { should have_selector('input', type: 'submit') }
 end
 end