这是以下环境中控制器规范中的cookie集合的问题:
我有一个简单的控制器规范,它创建一个Factory用户,调用一个设置cookie的登录方法,然后测试以查看登录用户是否可以访问页面.问题是所有cookie似乎都在设置的身份验证cookie和我的控制器上调用的"show"操作之间消失.
在rails dev服务器上的浏览器中运行时,我的代码工作正常.运行规范时更奇怪的行为是,通过cookie哈希设置的所有内容都会消失,但通过会话哈希设置的所有内容仍然存在.我只是错过了使用rspec时cookie的工作原理吗?
规格代码
it "should be successful" do
@user = Factory(:user)
test_sign_in @user
get :show, :id => @user
response.should be_success
end
Run Code Online (Sandbox Code Playgroud)
登录代码
def sign_in(user, opts = {})
if opts[:remember] == true
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
session[:foo] = "bar"
cookies["blah"] = "asdf"
self.current_user = user
#there are two items in the cookies collection and one in the session now
end
Run Code Online (Sandbox Code Playgroud)
对get:show请求的身份验证检查在此失败,因为cookies [:auth_token]为nil
def current_user
#cookies collection is now empty …Run Code Online (Sandbox Code Playgroud) 我有这个代码
if @temp_user.save
sign_in(:user, @temp_user)
render text: "OK"
else
render text: render_to_string(:partial => "errors")
end
Run Code Online (Sandbox Code Playgroud)
我尝试用rspec验证渲染"OK"
这是我的实际规格:
it "render text OK" do
post :create, {:agent => valid_attributes}
# response.should have_content("OK")
response.should render_template(:text => "OK")
end
Run Code Online (Sandbox Code Playgroud)
但这个规范总是会响应0次失败,即使我把"OKI"放到位"OK"
有人有一个建议吗?
$ rails new [app name] -d postgresql --skip-test-unit每次我开始新的rails应用程序时,我都厌倦了打字.我正在使用rails 3.2.
有没有办法使用postresql和跳过测试单元我的新rails应用程序的默认值?
postgresql rspec rspec-rails rails-postgresql ruby-on-rails-3.2
我通过引入请求和响应模型来重构我的控制器,以便在演示之后执行一些挂在控制器周围的逻辑.我将所有响应和请求模型分别包含模块响应和请求.应用程序运行完美,但是当我运行测试时,我得到以下错误.
Failure/Error: Unable to find matching line from backtrace
RuntimeError:
Circular dependency detected while autoloading constant Responses::FolderContentResponse
Run Code Online (Sandbox Code Playgroud)
我的目录结构如下:
- app/
- models/
- responses /
注意:我已经看到了与此问题相关的问题,但是,他们的问题似乎与我的问题不相似.在我的情况下,它是随机发生的,只有在运行测试(RAILS TEST ENV)时,应用程序才能正常运行.
module Responses
class ContentResponse
include ActiveAttr::Model
#some attributes
#some methods
end
end
module Responses
class FolderContentResponse < ContentResponse
end
end
Run Code Online (Sandbox Code Playgroud)
FolderContent响应类继承自ContentResponse,后者具有FolderContent其他内容响应使用的更通用的方法.
我有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) 在我的rSpec测试中,我测试了paperclip将照片上传到我的系统并将其保存给给定用户的能力.
这很棒,但是在我的测试完成后,我的系统上有所有这些额外的文件.如何在完成测试套件后自动删除它.
谢谢迈克
在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
不确定如何进一步排除故障...任何帮助将不胜感激.
我刚刚开始使用RSpec(和Capybara)的功能规格.我正在测试我的ActiveAdmin仪表板,我想检查所有面板是否有一个订单表,如下面的代码段所示:
feature 'admin dashboard', type: :feature do
def panels
page.all('.column .panel')
end
describe 'all panels' do
it 'have an orders table' do
expect(panels).to all(have_css('table.orders tbody'))
end
end
end
Run Code Online (Sandbox Code Playgroud)
我all在单元测试中经常使用匹配器但是在包装Capybara的have_css匹配器时似乎没有用,因为我收到以下错误:
Failure/Error: expect(panels).to all(have_css('table.orders tbody'))
TypeError:
no implicit conversion of Capybara::RackTest::CSSHandlers into String
Run Code Online (Sandbox Code Playgroud)
我是否认为RSpec的内置all匹配器应该与其他匹配器一起工作?
注:我使用describe和it替代feature,并scenario在这种情况下,因为我测试输出,而不是用户交互方案(请参阅我的其他问题).
为了使用和测试我的网站的新响应前端,我正在尝试使用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
capybara ×3
ruby ×2
cookies ×1
factory-bot ×1
paperclip ×1
phantomjs ×1
poltergeist ×1
postgresql ×1
rspec2 ×1
web ×1