- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
执行 Michael Hartl 的 Rails 教程,位于 chapter 10, section 5, exercise 2 。我 - 非常简单 - 尝试编写测试以确保使用 will_paginate gem 来确保分页 div 出现在几个页面上(这似乎是 Hartl 测试分页是否有效的首选方法),但是当我添加以下代码时..
subject { page }
.
.
.
it { should have_selector('div.pagination') }
..它返回..
1) UserPages profile page
Failure/Error: it { should have_selector('div.pagination') }
expected css "div.pagination" to return something
这特别奇怪,因为在这个特定的 _spec 文件中,相同的 Rspec 代码在某些测试中通过,而在其他测试中失败(我将在下面强调这一点)。此外,pagination
div 也存在于我的浏览器的源代码中,当然它工作正常。
因为它在某些地方失败,而在其他地方失败,所以我必须假设这是与 will_paginate
相关的“范围”或赋值类型问题 - 你看,对于失败的测试,正在分页的内容实际上是“微帖子” Controller 的一部分,但正在测试的页面/ View 是由“用户” Controller 处理的。对于通过的测试, View 和 Controller 都是“用户”。
这可能是问题所在吗?也有可能我的 FactoryGirl 设置/调用已损坏,并且由于某种原因未触发测试中的分页代码。我是 Rails n00b - 实际上对编程完全陌生 - 所以谢谢你们。 :)
(另外,p.s.,如何让我的代码变得丰富多彩且漂亮?)
/spec/requests/user_pages_spec.rb
require 'spec_helper' # Omitted from below - I don't think this is relevant.
describe "UserPages" do
subject { page }
describe "index" do # This is the block where it is PASSING..
let(:user) { FactoryGirl.create(:user) } # I'll include my /spec/factories.rb file below
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all } # I'll omit my /app/models/user.rb file - I don't think it's relevant to the problem
before do
valid_signin user # References /spec/support/utilities.rb to sign in as a valid user, but I will omit this since this is part of the working tests
visit users_path # This is /app/views/users/index.html.erb - this is where SUCCESSFUL pagination testing occurs
end
describe "pagination" do
it { should have_selector('div.pagination') } #PASS!! As I would expect
.
.
.
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Awesome")} # Used for other tests
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Sauce") } # Used for other tests
before(:all) { 30.times { FactoryGirl.create(:micropost) } } # Is this constructed properly? Perhaps the posts are not created, and that's why the failure to render the paginate code in test.
after(:all) { user.microposts.delete_all } # I have tested with and without this line of code - it fails both ways.
before { visit user_path(user) } # /app/views/users/show.html.erb
it { should have_selector('div.pagination') } # FAIL!!! This is the line in question that generates the error above.
.
.
.
.
end
/app/views/users/index.html.erb(分页在网站上工作,测试也工作)
<%= provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
/app/views/users/show.html.erb(分页在网站上运行,测试失败)
<% provide(:title, @user.name) %>
<div class="row">
.
.
.
<div class="span8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %> # Here is where I declare that pagination should occur on @microposts, NOT @users
<% end %>
</div>
</div>
/spec/requests/factories.rb
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com" }
password "secret"
password_confirmation "secret"
factory :admin do
admin true
end
end
factory :micropost do
content "Lorem ipsum"
user
end
end
/Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'pg', '0.12.2'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
group :development do
gem 'guard-rspec', '0.5.5'
gem 'annotate', '~> 2.4.1.beta'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test, :development do
gem 'rspec-rails', '2.9.0'
end
group :test do
gem 'capybara', '1.1.2'
gem 'rb-fsevent', '0.4.3.1', :require => false
gem 'growl', '1.0.3'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'factory_girl_rails', '1.4.0'
end
编辑:找到this article related to will_paginate and view testing ,但不诚实地理解它(可能是由于语法)..它可能有某种关联吗?
最佳答案
当您使用时:
before(:all) { 30.times { FactoryGirl.create(:user) } }
您已经创建了一个用户,因此总计数达到 31,因此它会进行分页(使用 will_paginate 分页默认情况下每页显示 30 条记录)。
尝试创建 31 个微帖子而不是 30 个,应该会通过。
before(:each) { 31.times { FactoryGirl.create(:micropost, user: user) } }
编辑:我忘记将 :user 传递给微博工厂,它现在应该可以工作了(这是我测试中的代码并且它通过了)
关于ruby-on-rails - 使用 Capybara/Rspec 通过 will_paginate Gem 测试 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10762896/
我有一个文件字段,其中包含 opacity: 0并且正在重叠一个假按钮。它是一种常见的 css 技术,可以伪造一种在不同浏览器中一致显示的“上传按钮”。 Capybara 不允许我打电话 attach
我正在尝试为我的 ROR 应用程序编写一些 capybara cucumber 脚本,但发现很难通过每次都进行更改并运行完整功能来使脚本正确。 是否有适用于 Capybara 的 REPL(Read
像这样的html 我的代码是 attach_file("ok","./fileset/publisher/upload_pic.jpg") 但我失败了: 失败: Capybara::Element
我想在我的代码中设置不同的 Capybara 等待时间,具体取决于它们通常需要多长时间才能完全加载?我是否必须重复更改 Capybara.default_wait_time 还是有更好的方法? 最佳答
在 Capybara 中,是否可以在新窗口而不是当前窗口中打开链接? 最佳答案 如果此元素是链接,您只需获取其 href 并在新窗口中打开它: url = find('.some_link')[:hr
对于编辑记录的集成测试,我尝试用新文本替换表单输入字段中的现有文本: find("input[@id='course_title']").set("a safer workplace") 但是,每次我
我有一个动态生成的表单,如下所示: Do you like Pizza? [ ] Yes [ ] No HTML 看起来像这样: Do you like Pizza?
我正在尝试填写两个标有 id="admin_passsword"的文本框。我可以轻松访问第一个文本框,但由于除了占位符和上面的标签之外没有任何区别,我不知道如何访问第二个文本框来填写该字段。
我是一个新手,尝试使用 Capybara 测试我的 Rails 项目,但是当我尝试从我的 div 中检测一些字符串时,我对 page 和 page.body 的含义感到困惑:(in :js=>true
我在使用 rspec、capybara、capybara-webkit 和 timecop 的某些集成规范中看到错误。 Capybara::FrozenInTime: time appears
我使用以下 html 堆栈创建了自定义上传表单: 文件字段通过 display: none 的 css 隐藏属性(property)。因此,当她单击标签(自定义样式)时,用户会调用文件
假设我在 Mac OS X 上安装了 PhantomJS,我可以编写一个普通的旧 Ruby 脚本(没有 Cucumber,没有 RSpec)来驱动 Poltergeist 吗?换句话说,我想要一些 g
我希望单击我的测试应用程序上出现的弹出消息(如果存在)。我对 capybara 很陌生,似乎找不到办法做到这一点。我以前有过使用 watir 的经验,如果我使用 watir 的话,会是这样的: if
我目前使用 Watir-webdriver 进行所有前端测试,但开发团队使用 Capybara 在 Jenkins CI 上运行测试。我们都使用相同的 Cucumber 功能。 值得我们进行两次有效的
Capybara的API似乎不支持配置HTTP代理。有没有什么方法可以和它一起使用? 上下文:我使用 capybara 和 cucumber 来测试 Rails 应用程序,并使用 akephalos
我很好奇是否有更好的方法来测试页面中是否存在完整的 URL(包括协议(protocol))。 使用 Capybara 2.10.2 和 Rails 5.0.0.1,我有以下设置: rails_help
我有以下测试: it 'shows the current quantity of items inside cart' do item = create(:item) visit
我正在使用 Capybara 自动截取一些屏幕截图。 我需要 chrome 浏览器最大化运行,但我似乎无法让它工作。 基于 https://sites.google.com/a/chromium.or
下面是两个单选按钮的 html 代码,它们只是“值”属性不同 我想做的是选择“value =2”的单选按钮 我尝试使用“选择(“AmountOption”)”选择第一个单选按钮,但我想选择第二个单
我尝试使用几种变体来检查 Capybara 的 200 Ok HTTP 响应,但没有一个变体不起作用: response.should be_success page.status.should be
我是一名优秀的程序员,十分优秀!