我有2个型号,User和Bucket.User has_many Buckets和Bucket belongs_toa User.
在factories.rb,我有:
Factory.define :user do |user|
user.email "teste@test.com"
user.password "foobar"
user.password_confirmation "foobar"
end
Factory.sequence :email do |n|
"person-#{n}@example.com"
end
Factory.define :bucket do |bucket|
bucket.email "user@example.com"
bucket.confirmation false
bucket.association :user
end
Run Code Online (Sandbox Code Playgroud)
我有一个login_user模块,如下所示:
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = Factory.create(:user)
#@user.confirm!
sign_in @user
end
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Spork和Watch,我Buckets_controller_spec.rb很简单:
describe "User authenticated: " do
login_user
@bucket = Factory(:bucket)
it "should get index" do
get 'index' …Run Code Online (Sandbox Code Playgroud) 我把我的规格放在lib文件夹下的模块中spec/lib,但我既rspec没有运行也没有得到测试spec spec/的规格spec/lib.
有什么需要配置吗?我正在使用rails 3.2.0,以下是我的spec_helper.rb
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# These lines are needed for SimpleCov to generate a complete …Run Code Online (Sandbox Code Playgroud) 在Rails 3.1.2项目(MAC OS X)的工作,我有PhantomJS正确安装(我可以像下面运行的代码和它的作品完美,准确地抓住了页面的标题和保存准确的截图)
try_phantom.coffee
page = require('webpage').create()
page.open 'http://localhost:5000/parties/onetestparty', (status) ->
title = page.evaluate -> document.title
console.log "Title: #{title}"
page.render './log/javascript_screenshot.png'
phantom.exit()
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在rspec中使用capybara/poltergeist时如下:
spec_helper.rb
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Run Code Online (Sandbox Code Playgroud)
然后使用需要javascript的调用规范:
parties_spec.rb
it "should allow a simple screenshot", js: true do
visit "/"
page.driver.render('./log/screen_Home.png', :full => true)
end
Run Code Online (Sandbox Code Playgroud)
它似乎没有呈现我的javascript,而且屏幕截图始终是空白的!
我已经尝试了调试器,但这似乎也带来了一个空白的HTML页面(只是带有空头和身体标签的html)
我很确定这个问题要么是水豚与恶作剧之间的界面,要么是(更有可能)恶作剧和幻影.以下是相关宝石的版本:
capybara 1.1.3 capybara
-webkit 0.13.0
poltergeist 1.0.2
phantomjs是1.7.0
不确定如何进一步排除故障...任何帮助将不胜感激.
我的模型中有一个私有方法,如下所示:
validate :record_uniq
private
def record_uniq
if record_already_exists?
errors.add(:base, "already exists")
end
end
def record_already_exists?
question_id = measure.question_id
self.class.joins(:measure).
where(measures: {question_id: ques_id}).
where(package_id: pack_id).
exists?
end
Run Code Online (Sandbox Code Playgroud)
这种方法更像是uniqueness防范重复记录的范围.我想知道如何validate :record_uniq使用shoulda或编写测试 rspec?
describe Foo do
before do
@bar = Foo.new(enr_rds_measure_id: 1, enr_rds_package_id: 2)
end
subject { @bar }
it { should validate_uniqueness_of(:record_uniq) }
end
Run Code Online (Sandbox Code Playgroud) 我试图将我的应用程序从Rails 3.2.x移植到Rails 4.0.4.所有的宝石都已经兼容,我正处于修复失败测试的阶段.
我有这种奇怪的测试失败.
我的 routes.rb
resources :my_reports, only: [:index] do
collection do
get "/report/:filename", to: :show, prefix: "pri/excl/rep", as: :show
end
end
Run Code Online (Sandbox Code Playgroud)
我的规范已经在Rails 3.2.x中传递,现在在更新到4.0.4后失败了
describe MyReportsController do
describe "#show" do
def make_request
get :show, prefix: 'some/place', filename: 'foo', format: 'doc'
end
it "makes a simple request" do
make_request
end
end
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Failure/Error: get :show, prefix: 'some/place', filename: 'foo', format: 'doc'
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"my_reports",
:filename=>"foo", :format=>"doc", :prefix=>"some/place"}
Run Code Online (Sandbox Code Playgroud)
我被困在这一点,欢迎提示.我正在使用rspec和rspec-rails版本2.14.1.
提前致谢!Sidekiq工作正常,但我无法用Devise Async测试它,或者我应该说我不能测试后者?
根据Sidekiq的文档,当测试模式设置为fake!时,给予worker的任何作业都会被推送到名为jobs同一个worker 的数组中.因此测试这个数组的增加是微不足道的.
但是,对于Devise Async来说,虽然它的后端包括,但它并不是那么微不足道Sidekiq::Worker.这是我尝试测试的一小部分内容:
Devise::Async::Backend::Sidekiq.jobsDevise::Mailer.deliveriesActionMailer::Base.deliveriesDevise::Async::Backend::Worker.jobs这些测试对象都没有指出尺寸的增加.由于Devise将其电子邮件作为模型回调发送,我尝试在模型和控制器规范中进行测试.使用Factory Girl和Database Cleaner,我也尝试了两种模式:事务和截断.毋庸置疑,我也尝试过两种Sidekiq模式:假的!和内联!
我错过了什么?
我需要在Rspec中理解这行代码.
create(:practice, creator: create(:physician, password: "password123", password_confirmation: "password123" ), phone: "+1 (555) 555-5554", office: "+1 (555) 555-5555", clinic_key: "abc123")
Run Code Online (Sandbox Code Playgroud)
什么是这个创建功能.它不是内置轨道或ruby功能.我们有文件吗?
我有一个只有Rails 5 API的应用程序,并使用knock进行JWT身份验证.
完成模型和模型规范后,我开始执行请求规范.
但我不知道如何以正确的方式完成请求规范内的身份验证,
我的用户控制器,
module V1
class UsersController < ApplicationController
before_action :authenticate_user, except: [:create]
end
end
Run Code Online (Sandbox Code Playgroud)
应用控制器,
class ApplicationController < ActionController::API
include Knock::Authenticable
include ActionController::Serialization
end
Run Code Online (Sandbox Code Playgroud)
我最愚蠢的解决方案(在休息请求之前调用get令牌请求获取JWT),
context 'when the request contains an authentication header' do
it 'should return the user info' do
user = create(:user)
post '/user_token', params: {"auth": {"email": user.email, "password": user.password }}
body = response.body
puts body # {"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODgxMDgxMDYsInN1YiI6MX0.GDBHPzbivclJfwSTswXhDkV0TCFCybJFDrjBnLIfN3Q"}
# use the retrieved JWT for future requests
end
end
Run Code Online (Sandbox Code Playgroud)
任何建议表示赞赏.
为了使用和测试我的网站的新响应前端,我正在尝试使用Rails的新系统测试(规格)与javascript和Chrome无头.不过,我无法想出一种在规范中设置浏览器屏幕大小的方法.
这是我的设置 spec/rails_helper.rb
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless, screen_size: [1900, 800]
end
Run Code Online (Sandbox Code Playgroud)
然后我创建截图:
page.driver.save_screenshot(the_uri)
Run Code Online (Sandbox Code Playgroud)
但是渲染屏幕截图的大小仍然是默认值,它要小得多.理想情况下,我希望看到整个渲染页面,但在这一点上,我很高兴只使用我提供的尺寸.
关于我在这里失踪的想法?
我正在测试我的控制器动作以进行练习。在我的控制器中,我只想从数据库中按名称获取所有不同的产品:
def shop
@products = Product.select('distinct on (name) *').sort_by &:order
end
Run Code Online (Sandbox Code Playgroud)
我已经手动检查过了,效果很好。现在,我使用我的RSpec设置测试,我想测试@products是一个大于0的数组:
RSpec.describe PagesController, type: :controller do
describe 'GET #shop' do
it 'should get all proudcts' do
get :shop
expect(assigns(:products).count).to be > 0
end
end
end
Run Code Online (Sandbox Code Playgroud)
现在,我尝试了期望的几种不同组合...但是它一直告诉我它的值为nil或0,但我知道不是。如何测试一个数组大于0?
rspec-rails ×10
rspec ×6
rspec2 ×2
ruby ×2
capybara ×1
devise ×1
devise-async ×1
factory-bot ×1
phantomjs ×1
poltergeist ×1
shoulda ×1
sidekiq ×1
upgrade ×1