只有当注册验证失败时才有三种方案,那么是否有更好的方法来表示它们,而不是有4种方案?我不想创建一个模型文件夹,所以请不要建议。
是否有更好的方法使用Capybara在Rails上表示这个RSpec代码?
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
发布于 2014-12-03 22:30:05
与其为3个不同的电子邮件案例创建3个长注册功能,您还可以做如下操作:
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
这将与注册过程相同,因为在您处理用户创建的两个过程中。
还有@托克兰的回答。
我认为最好不要重蹈覆辙:
it "......." do
expect(page).to ......
end
但是,只需在顶部的前块后面添加subject {page}
即可。它可以让你写得就像:
describe "....." do
before {visit root_path}
it {should have_content('Desired content'}
end
发布于 2014-12-03 17:20:14
一些评论:
feature
和“场景”连接起来,形成一个人类可读的文本。before
块中执行操作,只在it
块中执行断言。before
块来保持代码的干爽。root_path
。我就是这样写登录功能的,同样的想法也适用于Signup:
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
发布于 2014-12-03 19:43:31
我认为你的一些期望可能会导致脆弱的测试。我会稍微修改一下人们的期望。例如:
expect(page).to have_content('Logged in successfully')
如果您将“登录成功”改为“欢迎您,伙计!”或“祝您圣诞快乐,穆罕默德”,会发生什么?即使代码是正确的,您的测试也会失败。
“登录成功”是您规范的一部分吗?或者是您的规范“用户应该注册并有一个会话。”
我认为在页面上寻找一个特定的CSS类,以及其他类,(如果用户被登录的话,应该或者不应该存在的链接类)可能更健壮。我甚至会寻找链接文本,如“登录”和“注销”。
当然,这些也可以改变,甚至CSS类也是如此。但它们不太可能改变给用户的消息。
https://codereview.stackexchange.com/questions/71493
复制相似问题