我使用的是运行Snow Leopard 10.6.8和RVM 1.10.3,Ruby 1.9.3-p125,Ruby on Rails 3.2.2和rspec-rails-2.8.1的Mac OS.我已经正确安装并配置了所有宝石,但是在终端窗口中我运行rake spec命令我得到以下内容:
$ rake spec
/<ABSOLUT_PATH>/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/cli/command.rb:10: [BUG] Segmentation fault
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin10.8.0]
-- Control frame information -----------------------------------------------
c:0045 p:0011 s:0136 b:0136 l:000135 d:000135 CLASS /<ABSOLUT_PATH>/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/cli/command.rb:10
c:0044 p:0011 s:0134 b:0134 l:000133 d:000133 CLASS /<ABSOLUT_PATH>/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard/cli/command.rb:4
c:0043 p:0021 s:0132 b:0132 l:000131 d:000131 TOP /<ABSOLUT_PATH>/.rvm/gems/ruby-
1.9.3-p125/gems/yard-0.7.5/lib/yard/cli/command.rb:3
c:0033 p:0220 s:0104 b:0104 l:0020f0 d:0020f0 TOP /<ABSOLUT_PATH>/.rvm/gems/ruby-1.9.3-p125/gems/yard-0.7.5/lib/yard.rb:57
c:0032 p:---- s:0101 b:0101 l:000100 d:000100 FINISH
c:0031 p:---- s:0099 b:0099 l:000098 d:000098 CFUNC :require
c:0030 p:0026 …Run Code Online (Sandbox Code Playgroud) 我正在尝试开始测试ActiveAdmin,特别是我需要从一个ActiveAdmin控制器测试一个member_action.
你们知道关于这个主题的任何好的教程吗?
谢谢,
我正在尝试使用Minitest查找测试控制器的示例,但我只发现了一对,只是验证了呈现的模板以及返回的"成功".这对我来说似乎没什么帮助.Minitest用于测试控制器吗?
Railscast(http://railscasts.com/episodes/327-minitest-with-rails)和我发现的其他几篇文章似乎都在使用Minitest进行模型测试,并与Capybara进行集成测试.
控制器测试怎么样?他们可以用Minitest测试吗?如果是这样,是否有任何实际测试操作的真实示例?在经过几个小时的搜索之后我找不到任何东西似乎很奇怪.
我知道这很模糊,但是我想决定是否应该使用RSpec或Minitest,但是如果不知道如何使用Minitest真正测试控制器,我不知道它是如何真正的选择,但我继续阅读一些关于它的一般好评如潮.
我最近从RSpec 2.99升级到RSpec 3.这将是我的规格之一:
require 'spec_helper'
describe User, :type => :model do
it "is invalid without a password" do
expect(FactoryGirl.build(:user, :password => nil).errors_on(:password).size).to eq(1)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我已经运行了Transpec gem,它应该将我的大部分规格转换为RSpec 3语法.但是,我仍然收到此错误(以及其他一些错误):
Failure/Error: expect(FactoryGirl.build(:user, :password => nil).errors_on(:password).size).to eq(1)
NoMethodError:
undefined method `errors_on' for #<User:0x00000108beaba0>
Run Code Online (Sandbox Code Playgroud)
我尝试以多种不同的方式重新编写测试,但错误不会消失.
有人可以帮忙吗?
使用Rspec在Rails控制器中测试强params过滤的实际策略是什么?(除了应该匹配)如何编写失败的测试然后使其变为绿色?
我正在使用rspec-rails 2.12.0和capybara 2.0.1进行测试.在capybara 2.x中,您需要将您的规格放在spec/features而不是spec/requests中.有没有办法,如果我要生成脚手架ala'rail g scaffold Model',rspec会在正确的目录中为我生成功能规格?
我试图测试一个方法是否使用RSpec在我的Rails 3.2中触发电子邮件.它在生产和开发中起作用,我无法让它通过我的测试.
我收到了错误 undefined method 'deliver' for nil:NilClass
# user_spec.rb
it "should send an email if user has weekly email set to true" do
@user = FactoryGirl.create(:user, user_name: "Jane Doe", email: "jane@gmail.com", weekly_email: true, weekly_article_count: 10)
Mailer.should_receive(:weekly_email)
User.send_weekly_email
end
# user.rb
scope :weekly_email, where(:weekly_email => true)
scope :weekly_article_count, :conditions => ["weekly_article_count IS NOT NULL"]
def self.send_weekly_email
@users = User.weekly_email.weekly_article_count
@users.each do |user|
Mailer.weekly_email(user).deliver
end
end
Run Code Online (Sandbox Code Playgroud)
我也试过使用Mailer.should_receive(:weekly_email).with(user),但后来我收到了错误undefined local variable or method 'user'
我的团队在CI流程中使用coveralls.io为我们提供了一个rspec覆盖率分数.我们还将ActiveAdmin gem用于内部使用,并且决定不在我们的测试覆盖范围内涵盖ActiveAdmin功能.有谁知道我们如何从工作服中免除/ app/admin文件夹,以便它不会拖累我们的分数?
continuous-integration ruby-on-rails rspec-rails activeadmin
require './spec/spec_helper'
require './bank'
describe Bank do
context "#transfer" do
before(:all) do
@customer1 = Customer.new(500)
customer2 = Customer.new(0)
@customer1.stub(:my_money).and_return(1000)
customer2.stub(:my_money).and_return(0)
@transfer_message = Bank.new.transfer(@customer1, customer2, 2000)
end
it "should return insufficient balance if transferred amount is greater than balance" do
expect(@transfer_message).to eq("Insufficient funds")
end
it "calls my_money" do
expect(@customer1).to have_received(:my_money)
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我使用它before(:each)而不是before(:all)它的作用.但如果使用before(:all)它会抛出错误undefined method proxy_for for nil:NilClass.我找不出原因.请你帮助我好吗?提前致谢.
在过去的几天里,我一直在尝试测试这种方法,但是没有运气。
我想做的另一件事是rescue在尝试最后一次重试后冒出的错误。
请在下面查看我的评论和代码段。
这是示例代码和测试:
my_job.rb
retry_on Exception, wait: 2.hours, attempts: 3 do |job, exception|
# some kind of rescue here after job.exceptions == 3
# then notify Bugsnag of failed final attempt.
end
def perform(an_object)
an_object.does_something
end
my_spec.rb
it 'receives retry_on 3 times' do
perform_enqueued_jobs do
expect(AnObject).to receive(:does_something).and_raise { Exception }.exactly(3).times
expect(MyJob).to receive(:retry_on).with(wait: 2.hours, attempts: 3).exactly(3).times
MyJob.perform_later(an_object)
end
assert_performed_jobs 3
end
Run Code Online (Sandbox Code Playgroud)
测试失败响应:
1) MyJob.perform receives retry_on 3 times
Failure/Error: expect(job).to receive(:retry_on).with(wait: 4.hours, attempts: 3).exactly(3).times
(MyJob (class)).retry_on({:wait=>2 hours, …Run Code Online (Sandbox Code Playgroud) rspec ruby-on-rails rspec-rails rails-activejob ruby-on-rails-5.1
rspec-rails ×10
rspec ×9
activeadmin ×2
ruby ×2
testing ×2
capybara ×1
macos ×1
minitest ×1
rspec2 ×1