首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RSpec/Capybara试验

RSpec/Capybara试验
EN

Code Review用户
提问于 2014-12-03 08:50:57
回答 3查看 643关注 0票数 2

只有当注册验证失败时才有三种方案,那么是否有更好的方法来表示它们,而不是有4种方案?我不想创建一个模型文件夹,所以请不要建议。

是否有更好的方法使用Capybara在Rails上表示这个RSpec代码?

代码语言:javascript
运行
复制
feature 'Login' do
  before do
    FactoryGirl.create(:user)
  end

  scenario "success login", js: true do
    # set_speed(:slow)
    visit root_path
    click_link 'Login'

    fill_in 'email', :with => 'test@example.com'
    fill_in 'password', :with => 'password'
    click_button 'Login'

    expect(page).to have_content('Logged in successfully')
  end

  scenario "failed login", js: true do
    # set_speed(:slow)
    visit root_path
    click_link 'Login'

    fill_in 'email', :with => 'failed@login.com'
    fill_in 'password', :with => 'something failed'
    click_button 'Login'

    expect(page).to have_content('Invalid login/password combination')
  end
end

feature "Sign Up" do
  scenario "success sign up", js:true do
    visit root_path
    click_link 'Login'
    click_link 'Sign Up'

    fill_in 'user[email]', :with=>'signup@example.com'
    fill_in 'user[password]', :with=> 'password'
    fill_in 'user[password_confirmation]', :with=> 'password'
    click_button 'Create User'
    expect(page).to have_content('User successfully added.')
  end


  scenario "failed sign up/Wrong email format", js:true do
    visit root_path
    click_link 'Login'
    click_link 'Sign Up'  
    fill_in 'user[email]', :with=>'signup.example.com'
    fill_in 'user[password]', :with=> 'password'
    fill_in 'user[password_confirmation]', :with=> 'password'
    click_button 'Create User'
    expect(page).to have_content('is invalid')
  end

  scenario "failed sign up/Short Email address", js:true  do
    visit root_path
    click_link 'Login'
    click_link 'Sign Up'
    fill_in 'user[email]', :with=>'sign'
    fill_in 'user[password]', :with=> 'password'
    fill_in 'user[password_confirmation]', :with=> 'password'
    click_button 'Create User'
    expect(page).to have_content('is too short (minimum is 5 characters)')
  end 

  scenario "failed sign up/Long Email address", js:true  do
    visit root_path
    click_link 'Login'
    click_link 'Sign Up'
    fill_in 'user[email]', :with=>'fillinginwritingwronglongpasswordstogetanerror@gmail.com'
    fill_in 'user[password]', :with=> 'password'
    fill_in 'user[password_confirmation]', :with=> 'password'
    click_button 'Create User'
    expect(page).to have_content('is too long (maximum is 50 characters)')
  end 

end
EN

回答 3

Code Review用户

回答已采纳

发布于 2014-12-03 22:30:05

与其为3个不同的电子邮件案例创建3个长注册功能,您还可以做如下操作:

代码语言:javascript
运行
复制
describe "email is in wrong format" do
  let(:user) {FactoryGirl.create(:user)}
  before {user.email = something.with.wrongformat}
  it {should_not be_valid}
end

describre "too long email" do
  let(:user) {FactoryGirl.create(:user)}
  before {user.email = ("a"*60)+"@gmail.com"}
  it {should_not be_valid}
end

这将与注册过程相同,因为在您处理用户创建的两个过程中。

还有@托克兰的回答。

我认为最好不要重蹈覆辙:

代码语言:javascript
运行
复制
it "......." do
  expect(page).to ......
end

但是,只需在顶部的前块后面添加subject {page}即可。它可以让你写得就像:

代码语言:javascript
运行
复制
describe "....." do
  before {visit root_path}
  it {should have_content('Desired content'}
end
票数 2
EN

Code Review用户

发布于 2014-12-03 17:20:14

一些评论:

  • Rspec期望将feature和“场景”连接起来,形成一个人类可读的文本。
  • AFAIK,规范的正交结构是在before块中执行操作,只在it块中执行断言。
  • 使用before块来保持代码的干爽。
  • 个人观点:在一个功能规范中,你使用的应用程序内部越少,越好。所以我会写"/“而不是root_path
  • 工厂中的用户/密码信息a,我更喜欢创建的显式属性。

我就是这样写登录功能的,同样的想法也适用于Signup:

代码语言:javascript
运行
复制
feature 'Login' do
  before do
    FactoryGirl.create(:user, :email => "test@example.com", :password => "password")
    visit root_path
    click_link 'Login'
  end

  scenario "with valid user/password", js: true do
    before do
      fill_in 'email', :with => 'test@example.com'
      fill_in 'password', :with => 'password'
      click_button 'Login'
    end

    it "shows the logged in message" do
      expect(page).to have_content('Logged in successfully')
    end
  end

  scenario "with wrong user/password", js: true do
    before do
      fill_in 'email', :with => 'failed@login.com'
      fill_in 'password', :with => 'something failed'
      click_button 'Login'
    end

    it "shows an error message" do
      expect(page).to have_content('Invalid login/password combination')
    end
  end
end
票数 4
EN

Code Review用户

发布于 2014-12-03 19:43:31

我认为你的一些期望可能会导致脆弱的测试。我会稍微修改一下人们的期望。例如:

代码语言:javascript
运行
复制
  expect(page).to have_content('Logged in successfully')

如果您将“登录成功”改为“欢迎您,伙计!”或“祝您圣诞快乐,穆罕默德”,会发生什么?即使代码是正确的,您的测试也会失败。

“登录成功”是您规范的一部分吗?或者是您的规范“用户应该注册并有一个会话。”

我认为在页面上寻找一个特定的CSS类,以及其他类,(如果用户被登录的话,应该或者不应该存在的链接类)可能更健壮。我甚至会寻找链接文本,如“登录”和“注销”。

当然,这些也可以改变,甚至CSS类也是如此。但它们不太可能改变给用户的消息。

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/71493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档