- 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/
以下是一个非常简单的ruby服务器。 require 'socket' local_socket = Socket.new(:INET, :STREAM) local_addr = Socket.
我正在使用 OS X(使用 bash),并且是 unix 的新手。我想知道是否可以修改一些文件以便运行 ruby 程序,我不需要“ruby file.rb”,而是可以运行“ruby.rb”。 有理
我在用 Ruby 替换字符串时遇到一些问题。 我的原文:人之所为不如兽之所为。 我想替换为:==What== human does is not like ==what== animal does.
我想在一个循环中从 Ruby 脚本做这样的事情: 写一个文件a.rb(每次迭代都会改变) 执行系统(ruby 'a.rb') a.rb 将带有结果的字符串写入文件“results” a.rb 完成并且
我的问题是尝试创建一个本地服务器,以便我可以理解由我的新团队开发的应用程序。我的问题是我使用的是 Ruby 2.3.3,而 Gemfile 需要 2.3.1。我无法编辑 Gemfile,因为我被告知很
我有一个使用 GLI 框架用 Ruby 编写的命令行实用程序。我想在我的主目录中配置我的命令行实用程序,使用 Ruby 本身作为 DSL 来处理它(类似于 Gemfile 或 Rakefile)。 我
我的 Rails 应用 Controller 中有这段代码: def delete object = model.datamapper_class.first(:sourced_id =>
我正在寻找的解析器应该: 对 Ruby 解析友好, 规则设计优雅, 产生用户友好的解析错误, 用户文档的数量应该比计算器示例多, UPD:允许在编写语法时省略可选的空格。 快速解析不是一个重要的特性。
我刚开始使用 Ruby,听说有一种“Ruby 方式”编码。除了 Ruby on Rails 之外,还有哪些项目适合学习并被认可且设计良好? 最佳答案 Prawn被明确地创建为不仅是一个该死的好 PDF
我知道之前有人问过类似的问题,但是我该如何构建一个无需在前面输入“ruby”就可以在终端中运行的 Ruby 文件呢? 这里的最终目标是创建一个命令行工具包类型的东西。现在,为了执行我希望用户能够执行的
例如哈希a是{:name=>'mike',:age=>27,:gender=>'male'}哈希 b 是 {:name=>'mike'} 我想知道是否有更好的方法来判断 b 哈希是否在 a 哈希内,而
我是一名决定学习 Ruby 和 Ruby on Rails 的 ASP.NET MVC 开发人员。我已经有所了解并在 RoR 上创建了一个网站。在 ASP.NET MVC 上开发,我一直使用三层架构:
最近我看到 Gary Bernhardt 展示了他用来在 vim 中执行 Ruby 代码的 vim 快捷方式。捷径是 :map ,t :w\|:!ruby %. 似乎这个方法总是执行系统 Rub
在为 this question about Blue Ruby 选择的答案中,查克说: All of the current Ruby implementations are compiled to
我有一个 Ruby 数组 > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters"
我在四舍五入时遇到问题。我有一个 float ,我想将其四舍五入到小数点后的百分之一。但是,我只能使用 .round ,它基本上将它变成一个 int,意思是 2.34.round # => 2. 有没
我使用 ruby on rails 编写了一个小型 Web 应用程序,它的主要目的是上传、存储和显示来自 xml(文件最多几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程
我们如何用 Ruby 转换像这样的字符串: 𝑙𝑎𝑡𝑜𝑟𝑟𝑒 收件人: Latorre 最佳答案 s = "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" => "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" s.u
通过 ruby monk 时,他们偶尔会从左侧字段中抛出一段语法不熟悉的代码: def compute(xyz) return nil unless xyz xyz.map {|a,
不确定我做错了什么,但我似乎弄错了。 问题是,给你一串空格分隔的数字,你必须返回最大和最小的数字。 注意:所有数字都是有效的 Int32,不需要验证它们。输入字符串中始终至少有一个数字。输出字符串必须
我是一名优秀的程序员,十分优秀!