【问题标题】:hot to write a test with capybara用水豚写一个测试很热
【发布时间】:2014-01-01 22:27:38
【问题描述】:

这是我第一次使用 RSpec 和 Capybara 编写测试。这是我到目前为止所拥有的:

require 'capybara/rspec'

describe "the signin process" do
before :each do
User.make(:email => 'test@test.com', :password => 'thisisatest')
end

  it 'signs me in' do
    visit 'sessions/new'
    within("session") do
    fill_in 'user email', :with => 'test@test.com'
    fill_in 'password', :with => 'thisisatest'
  end
  click_link 'login'
  expect(page).to have_content 'Thank You'
  end
end

这是我运行测试时收到的消息:

the signin process
signs me in (FAILED - 1)

Failures:

1) the signin process signs me in
 Failure/Error: User.make(:email => 'yedidyaweiner@gmail.com', :password => 'Shabbos!78')
 NameError:
   uninitialized constant User
 # ./spec/features/sign_in_spec.rb:5:in `block (2 levels) in <top (required)>' 

Finished in 0.0006 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/sign_in_spec.rb:8 # the signin process signs me in

我该如何解决这个问题以使测试通过?关于如何更好地编写测试有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails rspec capybara integration-testing


    【解决方案1】:
    require 'spec_helper'
    describe "The signin process" do
      let!(:user) { User.create email: 'test@test.com', password: 'secret' }
    
      it 'signs me in' do
        visit 'sessions/new'
        within("session") do
          fill_in 'user email', with: user.email
          fill_in 'password',   with: user.password
        end
        click_link 'login'
        expect(page).to have_content 'Thank You'
      end
    end
    

    【讨论】:

    • 嗯,还是失败了。
    • 你能显示spec_helper.rb文件吗?一切都在浏览器中运行吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多