ruby-on-rails-3 - Rails 3 集成测试 - 使用 webrat fill_in 找不到字段

标签 ruby-on-rails-3 rspec integration-testing webrat

我现在正在学习测试,但是 Webrat 遇到了一些问题,无法使用 fill_in 查找表单字段即使我已经验证它在正确的页面上。 Webrat 是否可以使用字段名称或 ID?我曾尝试使用 Ruby 符号和表单名称来访问该字段,但在我的情况下都不起作用。你看到我的实现有什么不正确的吗?

错误信息:

5) Forwarding should forward the user to the requested page after signin
   Failure/Error: integration_sign_in(user)
   Could not find field: :email

测试代码:
it "should forward the user to the requested page after signin" do
  user = Factory(:user)
  visit edit_user_path(user)

  # The test automatically follows the redirect to the signin page
  puts current_url
  integration_sign_in(user)

  # The test follows the redirect again, this time to users/edit
  response.should render_template('users/edit')
end

哪里integration_sign_inspec_helper.rb
def integration_sign_in(user) 
  fill_in :email,   :with => user.email 
  fill_in :password, :with => user.password 
  click_button
end

表单字段 HTML:
<form action='/sessions' class='mtm' id='sign-in-form' method='post'> 
    <input name='authenticity_token' type='hidden' value='iIIqT6bUwiJkpqpgxm5sjAj3egrNcEgeXPsYmbKQ02U='> 
        <div class='landingSignInForm'> 
          <label class='mas signin-label' for='email'>E-mail:</label> 
          <input class="mls ras" id="email" name="email" placeholder="e-mail address" type="text" /> 
          <label class='mas signin-label' for='password'>Password:</label> 
          <input class="mls ras" id="password" name="password" placeholder="password" type="password" /> 
          <input checked='checked' class='mls mtm' name='remember' type='checkbox' value='permanent'> 
          <span class='remember-me-label'>Keep me signed in</span> 
          <input class='mls mvm ram medium silver button' name='submit' type='submit' value='Sign in'> 
          <a class='forgot-password' href='#'>Forget your password?</a> 
        </div> 
</form> 

最佳答案

您是否尝试过使用 css 选择器作为 ID 而不是仅传递匹配的符号?我尝试简要阅读 webrat 源代码以确定它是否在内部将符号视为字符串,想知道这是否是您的问题。我找不到任何使用 fill_in :symbol 的 webrat 语法示例只有fill_in 'string'
尝试这个:

def integration_sign_in(user) 
  fill_in 'email',   :with => user.email 
  fill_in 'password', :with => user.password 
  click_button
end

或您的 id 的 css 选择器路由:
def integration_sign_in(user) 
  fill_in '#email',   :with => user.email 
  fill_in '#password', :with => user.password 
  click_button
end

关于ruby-on-rails-3 - Rails 3 集成测试 - 使用 webrat fill_in 找不到字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5326256/

相关文章:

ruby - 列出 Rails Controller 实例变量

ruby-on-rails - 计算 table 的大小并设置为 cucumber capybara

java - Spring 启动 : testing paged results using TestRestTemplate

java - 如何使用@SpringBootTest 在 Spring 中运行集成测试

unit-testing - 如何避免集成测试中的业务逻辑重复

ruby-on-rails-3 - 仅在Heroku上完成302在Rails 3中的注册过程

ruby-on-rails - require_tree 是否需要 'vendor/assets' 和 'lib/assets' 内的文件? - rails

ruby-on-rails - 为什么我的 rails 路线有点偏离?

ruby-on-rails - 如何检查 Controller 中私有(private)方法的返回值

ruby-on-rails - Ruby on Rails 教程 : RSpec test failing when refactoring with matcher