- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我创建了一个规范来测试用户如何确认他的帐户。它在以下场景中测试用户:
虽然下面这段代码完全未经测试(全部在浏览器中编写),但我不明白为什么它不起作用。然而,这些是我的担忧:
同样,这段代码甚至从未见过 sublime text 的光芒,所以可能会有一些小问题:
更干燥但更难遵循:
describe 'user spec', js: true do
before { visit root_path }
let(:user){ FactoryGirl.build(:user) }
module Helpers
def deliveries
ActionMailer::Base.deliveries
end
def last_email
deliveries.last
end
def sent_emails_count
deliveries.count
end
def reset_action_mailer
deliveries = []
end
def reload_user(user_name)
User.find_by(user_name: user_name) # like to know how to write a method that could find by any attribute, not just username. E.g. `reload_user(email: 'thisismyemail@gotmail.com)`
end
def register_user(user)
click_link 'sign up today!'
fill_in 'user_user_name', with: user.user_name
fill_in 'user_email', with: user.email
fill_in 'user_password', with: user.password
fill_in 'user_password_confirmation', with: user.password_confirmation
click_button 'Register'
end
def sign_out(user) # I have a 'useless' user parameter in there to make code more self-documenting. E.g. sign_out(user_1), sign_out(user_2)
click_link 'sign out'
end
def sign_in(user)
click_link 'sign_in'
fill_in 'user_email', with: user.user_name
fill_in 'user_password', with: user.password
end
end
# shared examples
shared_examples_for 'a succeeded confirmation attempt' do |user|
describe 'the page' do
subject { page }
it_should_behave_like 'profile_page'
it{ should have_selector '.alert-success', text: "Thankyou for confirming! You're now a fully fledged user!" }
it{ should have_selector 'h4', text: 'confirmed' }
end
describe 'the instance' do
it_should_behave_like 'an confirmed instance', user
end
end
shared_examples_for 'a failed confirmation_attempt' do |user|
describe 'the page' do
subject { page }
it_should_behave_like 'profile_page'
it{ should have_selector '.alert-error', text: "Sorry, that key is wrong!" }
it{ should have_selector 'h4', text: 'unconfirmed' }
end
describe 'the instance' do
it_should_behave_like 'an unconfirmed instance', user
end
end
shared_examples_for 'an unconfirmed instance' do |user|
subject { reload_user(user.user_name) }
its(:confirmed_at){ should be_nil }
its(:state){ should eq 'unconfirmed' }
end
shared_examples_for 'a confirmed instance' do |user|
subject { reload_user(user.user_name) }
its(:confirmed_at){ should eq Time.now }
its(:state){ should eq 'confirmed' }
end
scenario 'user signs up' do
before do
register_user(user)
end
describe 'the page' do
subject { page }
it_should_behave_like 'profile_page' # shared example that tests content and title.
it{ should have_selector '.alert-success', text: "welcome to the site, #{user.user_name}! Please check your inbox for a confirmation email. You won't be able to do anything untill you confirm your account!" }
it{ should have_selector 'h4', text: user.email }
it{ should have_selector 'h4', text: user.user_name }
it{ should have_selector 'h4', text: 'unconfirmed' }
end
describe 'the instance' do
subject { User.last } #is this the best way to do this? I want to do `user.reload` but I can't reload user because the user is built in memory (doesn't go through ActiveRecord), and so doesn't have an id
its(:confirmation_key_created_at){ should eq Time.now }
its(:confirmation_key){ should be_base64 } # my own matcher. No, I'm not an autistic maths genius, just testing it's 64 characters long ;)
its(:state){ should eq 'unconfirmed' }
end
describe 'last email' do
subject { last_email }
# using email-spec matchers https://github.com/bmabey/email-spec
it { should have_subject "Hi #{user.user_name}, confirmation link enclosed!" }
it { should deliver_to user.email }
it { should have_body_text user.confirmation_key }
end
scenario 'then attempts confirmation by following the confirmation link in his email' do
before do
visit culminate_path(user.confirmation_key) # In order to closer emulate what the user would do, I would like to actually get the last email and use capybara to click the confirmation link, rather then emulating the click as I'm doing here. How could I do this?
end
it_should_behave_like 'a succeeded confirmation attempt'
end
scenario 'then attempts confirmation by following a made up link' do
let(:user_2) { FactoryGirl.build(:user) }
before do
sign_out(user)
register_user(user_2) # could use FactoryGirl.create but this is an integration test. I want to emulate real-world events as much as possible
sign_out(user_2)
sign_in(user)
visit culminate_path(SecureRandom.urlsafe_base64)
end
it_should_behave_like 'a failed confirmation_attempt'
end
scenario 'then attempts confirmation by following a confirmation link for a different user' do
let(:user_2) { FactoryGirl.build(:user) }
before do
sign_out(user)
register_user(user_2) # could use FactoryGirl.create but this is an integration test. I want to emulate real-world events as much as possible
sign_out(user_2)
sign_in(user)
visit culminate_path(user_2.confirmation_key)
end
it_should_behave_like 'a failed confirmation_attempt'
end
scenario 'then signs out and attempts confirmation by following the confirmation link in his email' do
let(:user_2) { FactoryGirl.build(:user) }
before do
sign_out(user)
visit culminate_path(user.confirmation_key)
end
describe 'the page' do
subject { page }
it_should_behave_like 'sign_in_page'
it{ should have_selector '.alert-error', text: "Sorry, you need to be signed in inorder to attempt confirmation!" }
end
describe 'the instance' do
it_should_behave_like 'a confirmed instance', user
end
scenario 'then signs in and visits link again' do
before do
sign_in(user)
visit culminate_path(user.confirmation_key)
end
it_should_behave_like 'a succeeded confirmation attempt'
end
end
end
end
更容易理解但不像 DRY:
describe 'user spec', js: true do
before { visit root_path }
let(:user){ FactoryGirl.build(:user) }
let(:user_2) { FactoryGirl.build(:user) }
# helper methods and shared examples hidden for brevity
scenario 'user signs up' do
before do
register_user(user)
end
describe 'the page' do
subject { page }
it_should_behave_like 'profile_page' # shared example that tests content and title.
it{ should have_selector '.alert-success', text: "welcome to the site, #{user.user_name}! Please check your inbox for a confirmation email. You won't be able to do anything untill you confirm your account!" }
it{ should have_selector 'h4', text: user.email }
it{ should have_selector 'h4', text: user.user_name }
it{ should have_selector 'h4', text: 'unconfirmed' }
end
describe 'the instance' do
subject { reload_user(user.user_name)} #is this the best way to do this? I want to do `user.reload` but I can't reload user because the user is built in memory (doesn't go through ActiveRecord), and so doesn't have an id
its(:confirmation_key_created_at){ should eq Time.now }
its(:confirmation_key){ should be_base64 } # my own matcher. No, I'm not an autistic maths genius, just testing it's 64 characters long ;)
its(:state){ should eq 'unconfirmed' }
end
describe 'last email' do
subject { last_email }
# using email-spec matchers https://github.com/bmabey/email-spec
it { should have_subject "Hi #{user.user_name}, confirmation link enclosed!" }
it { should deliver_to user.email }
it { should have_body_text user.confirmation_key }
end
end
scenario 'a user signs up and then attempts confirmation by submitting a made up confirmation key' do
before do
register_user(user)
visit culminate_path(SecureRandom.urlsafe_base64)
end
it_should_behave_like 'a failed confirmation_attempt'
end
scenario 'a user signs up and then attempts confirmation by following a confirmation link for a different user' do
before do
register_user(user_2)
sign_out(user_2)
register_user(user)
visit culminate_path(user_2.confirmation_key)
end
it_should_behave_like 'a failed confirmation_attempt'
end
scenario 'then signs in and visits link again' do
before do
sign_in(user)
visit culminate_path(user.confirmation_key)
end
it_should_behave_like 'a succeeded confirmation attempt'
end
scenario 'a user signs out and then attempts confirmation by following the confirmation link in his email' do
before do
register(user)
sign_out(user)
visit culminate_path(user.confirmation_key)
end
describe 'the page' do
subject { page }
it_should_behave_like 'sign_in_page'
it{ should have_selector '.alert-error', text: "Sorry, you need to be signed in inorder to attempt confirmation!" }
end
describe 'the instance' do
it_should_behave_like 'an unconfirmed instance', user
end
end
end
最佳答案
我认为您消除大部分重复是正确的。但是,我不会使用嵌套场景,因为它们需要读者在文件中跳来跳去。我也不会使用共享示例,因为它们模糊了控制流。
我会做这样的事情:
describe "user signup and confirmation" do
let(:user) { # whatever }
before do
register_user
visit root_path
end
scenario "user signs up" do
page_should_show_that_user_is_not_confirmed
user_should_not_be_confirmed
# Write assertions here to assert that confirmation email was sent
confirmation_url = confirmation_url_from_email
visit confirmation_url
page_should_show_that_user_is_confirmed
user_should_be_confirmed
end
scenario "user logs out before visiting confirmation URL" do
# no need to do the same assertions as we did in the previous scenario before the visit
confirmation_url = confirmation_url_from_email
sign_out user
visit confirmation_url
page_should_show_that_user_is_confirmed
user_should_be_confirmed
end
scenario "user attempts to sign up with an unknown confirmation key" do
visit culminate_path("some garbage")
page_should_show_that_user_is_not_confirmed
user_should_not_be_confirmed
end
scenario "user attempts to sign up with the wrong confirmation key" do
user2 = # whatever
visit culminate_path(user2.confirmation_key)
page_should_show_that_user_is_not_confirmed
user_should_not_be_confirmed
end
def page_should_show_that_user_is_not_confirmed
# Assertions
end
def page_should_show_that_user_is_confirmed
# Assertions
end
def user_should_not_be_confirmed
# Assertions
end
def user_should_be_confirmed
# Assertions
end
def confirmation_url_from_email
# Don't assert anything, just get the URL from the email
end
end
它总体上可读性比较好,我认为重复的内容刚好足以保持可读性。
关于ruby - DRY 与重复特征规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23297781/
我是 DRY principle 的坚定拥护者: Every piece of knowledge must have a single, unambiguous, authoritative rep
假设我有一个稍微复杂的 for 循环,用于不同的情况。有没有一种方法可以提取该 forloop 并仍然保持代码可读性? 例如: private function bar(){ for(i=0;
JSData 是替代还是补充 Sequelize.js? 我试图了解这两个库如何一起玩以及是否应该一起玩。 从我的阅读来看,JSData 主要处理访问数据,而 Sequelize.js 将创建和修改
我有 aws-eks 集群,下面是我替换现有配置的命令。 kubectl create configmap flink-config --from-file=./config -o yaml --dr
我正在设计一个可重用的类库,其中包含 2 个名为 core.xml.dll 和 core.string.dll 的程序集(其中包括)。 xml 程序集引用字符串程序集,以便使用一些字符串辅助方法。 但
我正在尝试找到一种更 DRY 的方式来使用 docker-compose env。 docker-compose-base.yml base: image: reactjs_web v
我有一些看起来像这样的类(class): struct A { void *stuff; int x; int foo() const; } 我有一些采用这种类型参数的函数,例如 int
我目前正在 Silverlight 中构建一些自定义控件。我希望这些控件能够响应验证错误。我想要做的是在我的控件周围设置红色边框,就像默认的 Silverlight 控件一样。 我的理解是我需要将它添
我正在处理一些服务器端代码,在将它们传递给客户端之前包装所有异常,因此所有面向客户端的方法都有以下代码 try{ DoSomething(); } catch (ExceptionA e) {
“纵深防御”原则指出,应该在多个地方实现约束,这样如果一条数据绕过或漏过一层,就会在下一层被捕获。一个很好的例子是在网络应用程序中——你将验证放在客户端 javascript 中,放在服务器端代码中(
使用这种方法在 MVC 中查看模型:http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view
我想将 jquery 脚本优化为多个元素(单个页面上有许多幻灯片)。我可以添加一些功能,但这并不干燥(不要重复自己)。 Pr
我有一些代码(不是我的),其中包含带有声明的范围映射的指令。我的愿望是在其他地方使用该指令并传递将在模板中使用的附加变量。 我要传递的变量是theVar。 我发现唯一可行的解决方案看起来很噩梦:
我将数据存储在数组中,并从该数组中使用循环创建表。对于一个表,我需要两列,另外 30 列(取决于数组项)。这将有三列。 var prodej = [ /*First column, second,
有没有更好的方法来声明来自同一组(例如“com.android.support”)具有相同版本(例如“23.4.0”)的多个软件包(例如“appcompat-v7”)? 实际申报: ... def a
我有一个看起来像这样的对象: var myObj = { _fooCon: function(f) { if (typeof f != 'function') throw
这就是我所拥有的。 Map data = new HashMap<>(); // assume this has been populated public int getLastestVersion
我在尝试维护 DRY 概念时遇到了 AngularJS 的问题 [不要重复自己]。我希望我做错了什么,有人可以指出我的错误。 一家公司销售卡车和汽车。 这两个项目具有相似和不同的属性。添加到其中一种的
我最终在几个类中得到了几个遵循相同模式的方法(每个类中总是有一堆这些方法): private void updateFoo() { String newFoo = fooTextField.g
假设我有一个字典config,其中包含键username和password。我想创建一个新字典,仅包含 config 中的username 和 password 键值对。一种方法是: new_dict
我是一名优秀的程序员,十分优秀!