新用户注册我的小应用程序必须得到管理员(我)的批准才能访问该网站.我已成功使用after_create :send_admin_email我的用户模型在开发中生成此类电子邮件,该模型非常有效.我的问题是我在测试期间(使用FactoryGirl)生成了多个用户,并且每个创建的测试用户都会发送一封真实的电子邮件.运行我的测试就像在1月份浇糖蜜,我必须删除发送到我收件箱的数百封电子邮件.我怎么把它关掉?
Rails指南中的Action Mailer Basics告诉我"默认情况下,Action Mailer不会在测试环境中发送电子邮件.它们只是添加到ActionMailer :: Base.deliveries数组中."
而且,config/environments/test.rb我有:
config.action_mailer.delivery_method = :test
Run Code Online (Sandbox Code Playgroud)
这是除了config/environment.rb:
# Configuration for using SendGrid on Heroku
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => 'app[my app number]@heroku.com',
:password => '[something super secret]',
:domain => '[let's get this party started!.com]',
:enable_starttls_auto => true
}
ActionMailer::Base.delivery_method = :smtp
Run Code Online (Sandbox Code Playgroud)
我确信我错过了一些简单而基本的东西.我已经搜索了相关问题和帖子处理如何测试ActionMailer实际发送电子邮件.
对任何想法或帮助提前谦卑的感激.
附录:回答类似问题的答案是否有可能在开发黄瓜测试时关闭ActionMailer电子邮件?我能够阻止发送疯狂的电子邮件.不过,我不得不添加ActionMailer::Base.delivery_method = :test几个rspec文件.有没有办法可以在全球范围内关闭它?有人对发生的事情有任何想法吗?
我正在使用rspec-sidekiqgem(https://github.com/philostler/rspec-sidekiq)帮助测试我正在编写的工作人员,但出于某种原因,我的测试仍然失败.
这是我的测试:
require 'spec_helper'
describe CommunicationWorker do
it { should be_retryable false }
it "enqueues a communication worker" do
subject.perform("foo@bar.com", "bar@foo.com", [1,2,3])
expect(CommunicationWorker).to have_enqueued_jobs(1)
end
end
Run Code Online (Sandbox Code Playgroud)
这是错误:
1) CommunicationWorker enqueues a communication worker
Failure/Error: expect(CommunicationWorker).to have_enqueued_jobs(1)
expected CommunicationWorker to have 1 enqueued job but got 0
# ./spec/workers/communication_worker_spec.rb:9:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
我将他们的低级别测试基于他们的维基上的示例,但它不适合我...任何理由为什么这不起作用?
鉴于我的API消费者需要发送客户HTTP标头,如下所示:
# curl -H 'X-SomeHeader: 123' http://127.0.0.1:3000/api/api_call.json
Run Code Online (Sandbox Code Playgroud)
然后我可以像这样在before_filter方法中读取这个标题:
# app/controllers/api_controller.rb
class ApiController < ApplicationController
before_filter :log_request
private
def log_request
logger.debug "Header: #{request.env['HTTP_X_SOMEHEADER']}"
...
end
end
Run Code Online (Sandbox Code Playgroud)
到目前为止很棒.现在我想使用RSpec测试这个,因为行为有变化:
# spec/controllers/api_controller_spec.rb
describe ApiController do
it "should process the header" do
@request.env['HTTP_X_SOMEHEADER'] = '123'
get :api_call
...
end
end
Run Code Online (Sandbox Code Playgroud)
但是,request在ApiController中收到的将无法找到标头变量.
same code使用HTTP_ACCEPT_LANGUAGE标头尝试时,它将起作用.自定义标头是否在某处过滤?
PS:网络上的一些例子使用request而不是@request.虽然我不确定当前Rails 3.2/RSpec 2.14组合中哪一个是正确的 - 但这两种方法都不会触发正确的行为,但两者都可以兼顾HTTP_ACCEPT_LANGUAGE.
如果控制器只响应javascript,你如何测试Rspec中的控制器?例如,这将是我的实际代码:
一些view.html.erb
link_to 'More Chips', add_chips_path, :remote => true
Run Code Online (Sandbox Code Playgroud)
chips_controller
def add_chips
Chips.create(:color => :red)
@chips = Chips.all
end
Run Code Online (Sandbox Code Playgroud)
add_chips.js.erb
$('#chip_count').html('<%=j render("chips/list", :chips => @chips) %>');
Run Code Online (Sandbox Code Playgroud)
Rspec测试
规格/控制器/ chips_controller_spec
it "should add chips" do
post :add_chips, :format => 'js'
end
Run Code Online (Sandbox Code Playgroud)
当我尝试使用RSpec发布到此时,我收到一个Missing Template错误,因为它正在发送HTML请求,但没有HTML视图.我试过传递一种格式,但似乎没有用.我知道我可以放入一个"虚拟"html视图来使它通过,但这似乎是一个黑客.
谢谢
我已经看到一些SO帖子解释了如何使用pry进入rspec测试并且能够做到这一点.一旦我到达断点,我就很难显示任何有用的信息.对于下面的代码,我想从pry控制台检查响应对象:
describe 'happenings' do
context "#index (GET /api/v1/flat_happenings.json)" do
before(:each) do
30.times { FactoryGirl.create(:flat_happening) }
get "/api/v1/flat_happenings.json"
end
describe "should list all flat_happenings" do
binding.pry
it { JSON.parse(response.body)["flat_happenings"].length.should eq 30 }
end
end
end
Run Code Online (Sandbox Code Playgroud)
关于如何做到这一点的任何想法?
在测试的Rails 4.2.0应用程序中rspec-rails,我提供了一个JSON Web API,它具有类似REST的资源,具有强制属性mand_attr.
我想测试一下,BAD REQUEST当POST请求中缺少该属性时,此API会回答HTTP代码400().(请参阅第二个示例.)我的控制器尝试通过抛出一个来生成此HTTP代码ActionController::ParameterMissing,如下面的第一个RSpec示例所示.
在其他 RSpec示例中,我希望通过示例(如果它们是预期的)挽救引发的异常或者命中测试运行器,因此它们会显示给开发人员(如果错误是意外的),因此我不想要去除
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
Run Code Online (Sandbox Code Playgroud)
来自config/environments/test.rb.
我的计划是在请求规范中包含以下内容:
describe 'POST' do
let(:perform_request) { post '/my/api/my_ressource', request_body, request_header }
let(:request_header) { { 'CONTENT_TYPE' => 'application/json' } }
context 'without mandatory attribute' do
let(:request_body) do
{}.to_json
end
it 'raises a ParameterMissing error' do
expect { perform_request }.to raise_error ActionController::ParameterMissing,
'param is missing or the value …Run Code Online (Sandbox Code Playgroud) 我在我的Line模型中有这个
validates :home_team, :uniqueness => { :scope => [:visiting_team, :event_datetime],
:message => "** DOUBLE EVENT **" }
Run Code Online (Sandbox Code Playgroud)
我的规格中有这个
describe Line do
it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) }
Run Code Online (Sandbox Code Playgroud)
我收到这个错误......
失败:
1) Line
Failure/Error:
it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) }
Did not expect errors to include "has already been taken" when home_team is set to "arbitrary_string", got error:
# ./spec/models/line_spec.rb:7:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
任何想法为什么失败?
我刚刚将我的Capybara Gem从版本1升级到2.1.0(最新版).基于Capybara自述文件,我将以下几行添加到Spork.prefork块中的spec_helper.rb
require 'capybara/rspec'
require 'capybara/rails'
Run Code Online (Sandbox Code Playgroud)
但是,我收到了一个错误
/home/user_1/.rvm/gems/ruby-1.9.3-p392/gems/capybara-2.1.0/lib/capybara/rails.rb:6:in `block (2 levels) in <top (required)>': uninitialized constant Rails (NameError)
Run Code Online (Sandbox Code Playgroud)
我是否因为水豚的工作正常而错过了什么?
params在测试Rails辅助方法时设置哈希值?我想设置params[:search] = 'my keyword search'在我的帮助方法中使用,然后从it示例块中调用它.
require 'rails_helper'
describe BooksHelper do
describe "#page_title_helper" do
let(:params) { {search: 'my keyword search'} }
it "should read the params hash" do
expect(helper.params[:search]).to eq "my keyword search"
end
end
end
Run Code Online (Sandbox Code Playgroud)
BooksHelper
def title_helper
if params[:search]
"Books related to #{params[:search]}"
else
"All Books"
end
end
end
Run Code Online (Sandbox Code Playgroud) 我已经阅读了我能找到的每个类似的问题,但仍然无法弄清楚我的问题.
# routes.rb
Rails.application.routes.draw do
resources :lists, only: [:index, :show, :create, :update, :destroy] do
resources :items, except: [:new]
end
end
Run Code Online (Sandbox Code Playgroud)
# items_controller.rb (excerpt)
class ItemsController < ApplicationController
...
def create
@list = List.find(params[:list_id])
...
end
...
end
Run Code Online (Sandbox Code Playgroud)
# items_controller_spec.rb (excerpt)
RSpec.describe ItemsController, type: :controller do
...
let!(:list) { List.create(title: "New List title") }
let(:valid_item_attributes) {
{ title: "Some Item Title", complete: false, list_id: list.id }
}
let!(:item) { list.items.create(valid_item_attributes) }
describe "POST #create" do
context "with valid params" do
it …Run Code Online (Sandbox Code Playgroud)