我试图在rspec中模拟一个数组(在应用程序中它是来自外部API的返回对象),但我不知道如何.
我试着像这样嘲笑它:
item = double("item")
item.stub(:[]) { :return_value }
Run Code Online (Sandbox Code Playgroud)
哪个有效,但后来我会得到:数组中每个值的return_value.还有另外一种方法吗?
我有一个使用 Rails 3 的 Rails 应用程序。
我添加rspec-rails到我的Gemfile:
group :development, :test do
gem 'rspec-rails'
end
Run Code Online (Sandbox Code Playgroud)
然后我跑bundle install。它显示了我的 gem 列表,所有 rspec gem 都在那里(核心、导轨等)。
然而,当我跑
rails g rspec:install
Run Code Online (Sandbox Code Playgroud)
这就是它返回的内容:
create .rspec
create spec
create spec/spec_helper.rb
Run Code Online (Sandbox Code Playgroud)
虽然我的应用程序中有模型和控制器,但它只是创建这些文件。为什么 Rspec 不创建规范文件?
我正在使用Devise并为用户删除自己帐户的情况编写测试但我仍然坚持如何调用确认框并单击确定.
这是链接和我的测试:
<p><%= link_to "Delete my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p>
Run Code Online (Sandbox Code Playgroud)
规格/请求/ users_spec.rb
scenario 'user deletes account' do
make_user_and_login
click_link('Account Settings')
page.should have_selector('title', :text => 'Account Settings')
click_link('Delete my account')
# Are You Sure?
# click OK in confirm box
# page.should etc.....
end
Run Code Online (Sandbox Code Playgroud)
怎么做?
我使用twitter bootstrap gem生成在索引页面中显示created_at列的视图.与脚手架一起生成的rspec测试使用stub_model来测试索引视图.这些测试失败,因为created_at列为nil.
就像stub_model生成虚拟id一样,它也可以将created_at列设置为Time.now.可以通过配置完成.或者我们是否需要在创建存根时每次指定它.
我的rspec代码中出现以下错误
undefined local variable or method `render'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
require 'spec_helper'
describe "messages/show.html.erb" do
it "displays the text attribute of the message" do
render
rendered.should contain("Hello world!")
end
end
Run Code Online (Sandbox Code Playgroud)
这是本书:下面的RSpec书是我添加到Gemfile的.
group :development , :test do
gem "rspec-rails", ">= 2.0.0"
gem "webrat", ">= 0.7.2"
end
Run Code Online (Sandbox Code Playgroud)
我没有将文件show.html.erb添加到views/messages中,但我应该收到另一个错误,而不是这个
奇怪的是,这在我的机器上工作了一段时间.我删除了项目并创建了另一个项目,现在它不起作用
我在编辑gemfile后发出了这些命令
bundle install
script/rails generate rspec:install
rake db:migrate
Run Code Online (Sandbox Code Playgroud) 这是我第一次使用rspec.我uninitialized constant FactoryGirl在跑步时得到了rspec.我通过运行,在控制台中尝试了rails c test,它正确识别FactoryGirl.
这是rspec的输出:
Failures:
1) basic API gives authentication token from username and password
Failure/Error: user = FactoryGirl.create(:user, last_name: last_name)
NameError:
uninitialized constant FactoryGirl
# ./spec/requests/api/v1/api_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 0.00069 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/api/v1/api_spec.rb:2 # basic API gives authentication token from username and password
Run Code Online (Sandbox Code Playgroud)
这是我的spec/spec_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test' …Run Code Online (Sandbox Code Playgroud) 我已经从Rails 3.2.13升级到Rails 4.0.0.我修复了所有弃用警告(除了secret_base_key),升级的database_cleaner,rspec,rspec-rails,capybara,selenium-webdriver和poltergeist到最新版本.
现在,当我通过rake或rspec运行我的规范时,rspec的输出最终会挂起,总是在功能测试(javascript one)上.test.log的输出是明确的:测试仍在运行,但控制台没有得到任何更新.
我在用:
关于如何让我的测试套件再次正常运行的想法?
尝试在一个简单的页面中测试一些功能,我在使用page.check时遇到此错误:
Failures:
1) searching for stores serving a taco and a sauce: Adobada tacos with Chile de Arbol sauce
Failure/Error: page.check 'Adobada'
Capybara::ElementNotFound:
Unable to find checkbox "Adobada"
# ./spec/views/store_search_spec.rb:8:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
这是我的HTML:
<%= simple_form_for :stores, url: stores_path, method: :get do |f| %>
<%= f.label :tacos, 'Select Tacos to Find: ', class: 'label' %>
<div class='taco-select checkboxes'>
<%= f.collection_check_boxes :tacos, Taco.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>
<%= f.label :salsa, 'Select Sauces …Run Code Online (Sandbox Code Playgroud) 我正在使用RSpec(Rails 4.2.5 上的3.4 )测试视图。我在CRUD控制器中使用了decent_exposure gem。decent_exposure使得定义命名方法变得容易,这些命名方法可用于我的视图并记住结果值。
但是我不明白如何在视图规范中添加这些方法。我尝试根据RSpec文档进行操作,但是会引发错误。
为什么不起作用?如何article在视图中存根方法?
我的控制器
class Account::ArticlesController < Account::BaseController
expose(:articles) { current_user.articles }
expose(:article, attributes: :article_params)
# POST /account/articles
# POST /account/articles.json
def create
respond_to do |format|
if article.save
format.html { redirect_to account_article_url(article), notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: account_article_url(article) }
else
format.html { render :new }
format.json { render json: article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /account/articles/1
# …Run Code Online (Sandbox Code Playgroud) 我正在学习RSpec。我只想在单个文件上运行rspec。我通过完成它
cd to/my/project
Run Code Online (Sandbox Code Playgroud)
和
rspec ./spec/parent/child/spec.rb
Run Code Online (Sandbox Code Playgroud)
但是我下达命令后
rspec --order rand:seed
Run Code Online (Sandbox Code Playgroud)
我的rspec从头开始运行所有测试。如何撤消该命令?
rspec-rails ×10
rspec ×6
capybara ×3
unit-testing ×2
devise ×1
factory-bot ×1
rspec2 ×1
rspec3 ×1
ruby ×1