我已经test-unit安装和rspec安装(沿-core,-expectations,-mocks和-rails版本2.6.x的).当我运行该命令时rails new foo,它用于test-unit生成测试存根文件而不是rspec.
有没有一个选项,我可以告诉rails使用rspec来生成测试?
我的控制器中有以下代码:
format.json { render :json => {
:flashcard => @flashcard,
:lesson => @lesson,
:success => true
}
Run Code Online (Sandbox Code Playgroud)
在我的RSpec控制器测试中,我想验证某个场景确实收到了成功的json响应,所以我有以下行:
controller.should_receive(:render).with(hash_including(:success => true))
Run Code Online (Sandbox Code Playgroud)
虽然当我运行我的测试时,我收到以下错误:
Failure/Error: controller.should_receive(:render).with(hash_including(:success => false))
(#<AnnoController:0x00000002de0560>).render(hash_including(:success=>false))
expected: 1 time
received: 0 times
Run Code Online (Sandbox Code Playgroud)
我是否错误地检查了回复?
我有一个带有适当标签的字段,我可以用水豚填写没有问题:
fill_in 'Your name', with: 'John'
Run Code Online (Sandbox Code Playgroud)
我想在填写它之前检查它的值,但无法弄明白.
如果我fill_in在以下行之后添加:
find_field('Your name').should have_content('John')
Run Code Online (Sandbox Code Playgroud)
该测试失败,尽管之前的填充工作正如我通过保存页面验证的那样.
我错过了什么?
页面网址就像/people?search=name
我使用current_path的水豚方法一样/people只返回.
current_path.should == people_path(:search => 'name')
Run Code Online (Sandbox Code Playgroud)
但它没有说
expected: "/people?search=name"
got: "/people"
Run Code Online (Sandbox Code Playgroud)
我们怎么能通过?有没有办法做到这一点?
什么时候我应该使用Rails应用程序的规格和黄瓜(前rspec故事)?当然,我知道如何工作和积极使用规范.但使用黄瓜仍然感觉很奇怪.我目前对此的看法是,当您为客户端实现应用程序并且不了解整个系统应该如何工作时,使用Cucumber很方便.
但是,如果我正在做自己的项目呢?在大多数情况下,我知道系统的各个部分是如何相互作用的.我需要做的就是写一堆单元测试.那时我需要黄瓜的可能情况是什么?
并且,作为相应的第二个问题:如果我写黄瓜故事,我是否必须编写规范?难道不是同一件事的双重测试吗?
integration-testing unit-testing rspec cucumber rspec-stories
我的大多数测试都提出了以下内容,我不明白为什么.所有方法调用都会引发"authenticate"错误.我检查了代码是否有一个名为"authenticate"的方法,但没有这样的方法.
1) Admin::CommentsController handling GET to index is successful
Failure/Error: get :index
undefined method `authenticate!' for nil:NilClass
# ./spec/controllers/admin/comments_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
124) PostsController handling GET for a single post should render show template
Failure/Error: get :show, :year => '2008', :month => '01', :day => '01', :slug => 'a-post'
undefined method `authenticate' for nil:NilClass
# ./app/controllers/application_controller.rb:18:in `set_current_user_for_model'
# ./spec/controllers/posts_controller_spec.rb:131:in `do_get'
# ./spec/controllers/posts_controller_spec.rb:140:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
该项目可以在那里找到=> https://github.com/agilepandas/enki,以防你想自己运行测试.
我有控制器:
class AccountController < ApplicationController
def index
end
private
def current_account
@current_account ||= current_user.account
end
end
Run Code Online (Sandbox Code Playgroud)
如何current_account使用rspec 测试私有方法?
PS我使用Rspec2和Ruby on Rails 3
我正在使用Rspec和Capybara.
我怎么写一个步骤来检查checkbox?我已经尝试check了价值,但它找不到我的checkbox.我不知道该怎么做,因为我实际上有相同的ID具有不同的值
这是代码:
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="61" name="cityID">
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="62" name="cityID">
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="63" name="cityID">
Run Code Online (Sandbox Code Playgroud) 在这种情况下如何单击第一个链接:
<div class="item">
<a href="/agree/">Agree</a>
</div>
<div class="item">
<a href="/agree/">Agree</a>
</div>
Run Code Online (Sandbox Code Playgroud)
within ".item" do
first(:link, "Agree").click
end
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching css ".item"
Run Code Online (Sandbox Code Playgroud)
没有within我得到这个错误:
Failure/Error: first(:link, "Agree").click
NoMethodError:
undefined method `click' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)