我正在开发一个rails项目,我使用CanCan来授权我的资源.当用户未登录并尝试提交"谈话"(通过ajax表单提交)时,CanCan会正确引发401 {"status":"error","message":"You must be logged in to do that!"}作为响应(我已在浏览器中使用firebug验证了这一点).但是,在我的测试中得到302响应代码而不是401:
class TalksController < ApplicationController
authorize_resource
def create
@talk = current_user.talks.build(params[:talk])
respond_to do |format|
if @talk.save
response = { :redirect => talk_path(@talk) }
format.html { redirect_to @talk, notice: 'Talk was successfully created.' }
format.json { render json: response, status: :created, }
else
format.html { render action: "new" }
format.json { render json: @talk.errors, status: :unprocessable_entity }
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
talks_controller_spec.rb:
describe TalksController do
describe "POST create" do
context "when not …Run Code Online (Sandbox Code Playgroud) 我正在研究ruby.railstutorial.org/ruby-on-rails-tutorial-book.我使用rails 3.2.7,spork,rspec,capybara,launchy和一些警卫:)
我在第3章中有一个非常奇怪的问题:测试:
似乎测试不适用于<head>-Tag中的内容.如果我将<title>-tag放在<body>-tag而不是head-tag中,它可以正常工作.当我把<h1>-tags放在<title>里面 - <head>标签时,它也有效.这很奇怪,不是吗?
请帮帮我搞清楚.
示例来自:ruby.railstutorial.org/chapters/static-pages#code:title_test:
it "should have the right title" do
visit '/static_pages/home'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Home")
end
Run Code Online (Sandbox Code Playgroud)
错误消息是:
失败:
1)静态页面主页应该有标题'Home'失败/错误:page.should have_selector('title',:text =>'| Home')Capybara :: ExpectationNotMet:期望找到带有文本的css"title" |主页"但没有比赛.还找到"",它匹配选择器但不是所有过滤器.#./spec/requests/static_pages_spec.rb:15:在'块(3级)中'
那个工作:
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
Run Code Online (Sandbox Code Playgroud)
呈现的HTML文件:
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | …Run Code Online (Sandbox Code Playgroud) 我试图在rspec中为ROR应用程序编写一些测试(不是代码覆盖,但不相关),至少需要别名描述和它.我可以将别名描述得很好,因为它位于顶层.但是,我无法得到任何其他工作.像这样的家伙:
module RSpec
module Core
class ExampleGroupMethods
alias :they :it
end
end
end
Run Code Online (Sandbox Code Playgroud)
我在spec文件中包含了这个,但我没有得到正确的模块路径.我查看了rspec代码库,但是我碰到了墙,所以我不认为我完全知道自己在做什么.任何提示或资源将不胜感激.
我在rspec和rspec-rails 2.11.0中遇到了一些令人困惑的行为.我已经在同事的应用程序上运行了2.7.1(在ruby 1.9.3上)
这些测试按预期工作(失败):
it "should not change i" do
i = 0
expect {
i = 2
}.not_to change { i }
end
it "should not change i" do
i = 0
expect {
i = 2
}.not_to change { i }.from( 0 )
end
Run Code Online (Sandbox Code Playgroud)
在两种情况下,失败消息都是"结果不应该改变,但确实从0变为2"
将期望中的"from"更改为不同的值莫名其妙地使其通过,而不是失败,无论在expect块中i的值发生了什么:
it "should not change i" do
i = 0
expect {
i = 2
}.not_to change { i }.from( 1 )
end
Run Code Online (Sandbox Code Playgroud)
我最近才升级到1.9.3,我可以肯定地说,如果我遇到它,我会注意到这种行为.谁能解释这个和/或我做错了什么?
我试图在我的rspec中调用rake任务.
require "rake"
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
rake['rake my:task'].invoke
Run Code Online (Sandbox Code Playgroud)
但我收到了错误
Failure/Error: rake['rake db:migrate'].invoke
RuntimeError:
Don't know how to build task 'rake db:migrate'
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何在rspec代码中调用rake任务.
任何帮助将受到高度赞赏.
我们的测试需要一段时间才能运行,并且总有5-10分钟的时间我们知道哪个测试失败了,但是在套件完成之前我们无法看到失败消息或回溯.在它们发生时看到回溯会更有效.这可能吗?
我在Rails中拥有这条(当然是可怕的)路线:
scope '/software' do post '/:software_id/:attachment_id/event/*event' => 'software#post_event', as: 'post_event' end
(我会更改它,但对于遗留API)
我正在为它编写RSpec测试.
rake routes 给我:
post_event POST /software/:software_id/:attachment_id/event/*event(.:format) api/version1301/software#post_event
我的测试看起来像这样:
describe "post_event" do
it "should respond with 204" do
params = {
attachment_id: @attachment.uid,
software_id: @license.id
}
post :post_event, params
response.code.should eq "204"
end
end
但是我收到以下路由错误:
Failure/Error: post :post_event, params
ActionController::RoutingError:
No route matches {:format=>"json", :name_path=>:api, :attachment=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :software_id=>"0000001", :attachment_id=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :controller=>"api/version1301/software", :action=>"post_event"}
# ./spec/controllers/api/version1301/software_controller_spec.rb:62:in `block (4 levels) in '
你如何使用通配符(事件)处理路由?
当我在我的ruby轨道应用程序上运行rspec测试时..
git@ruby-rails:~/gitlab$ sudo bundle exec rspec spec/controllers/public_spec.rb
No DRb server is running. Running in local process instead ...
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/markup.rb:222: warning: already initialized constant PREFORMATTED_TAGS
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/markup.rb:685: warning: already initialized constant MarkupGFM
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/sanitization.rb:9: warning: already initialized constant ELEMENTS
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/sanitization.rb:23: warning: already initialized constant ATTRIBUTES
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/sanitization.rb:46: warning: already initialized constant PROTOCOLS
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/sanitization.rb:52: warning: already initialized constant ADD_ATTRIBUTES
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/sanitization.rb:62: warning: already initialized constant REMOVE_CONTENTS
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/sanitization.rb:68: warning: already initialized constant TRANSFORMERS
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum/web_sequence_diagram.rb:7: warning: already initialized constant WSD_URL
/usr/local/lib/ruby/gems/1.9.1/bundler/gems/gollum-5dcd3c8c8f68/lib/gollum.rb:32: warning: already initialized constant VERSION
MiniTest::Unit::TestCase is …Run Code Online (Sandbox Code Playgroud) 我正在使用rspec-rails 3.0.1测试rails 4.1.0应用程序.rspec命令正在打印有关我在应用程序中使用的gem的大量警告.我在下面列出了一部分输出.我想知道是否有可能抑制这种情况.
/home/indika/Documents/rails/news_app/config/initializers/kramdown.rb:6: warning: method redefined; discarding old convert_img
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/kramdown-1.4.0/lib/kramdown/converter/html.rb:259: warning: previous definition of convert_img was here
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mail-2.5.4/lib/mail/network/delivery_methods/sendmail.rb:53: warning: shadowing outer local variable - to
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:16: warning: File.exists? is a deprecated name, use File.exist? instead
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:16: warning: File.exists? is a deprecated name, use File.exist? instead
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:16: warning: File.exists? is a deprecated name, use File.exist? instead
/home/indika/Documents/rails/news_app/spec/factories/news_articles.rb:1: warning: method redefined; discarding old ruby_iterator_code
/home/indika/Documents/rails/news_app/spec/factories/news_articles.rb:1: warning: previous definition of ruby_iterator_code was here
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:45: warning: instance variable @cleaners not initialized
/home/indika/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rouge-1.5.1/lib/rouge/regex_lexer.rb:136: warning: …Run Code Online (Sandbox Code Playgroud) 有时我的Rails应用程序的一些规格似乎随机失败,因为突然英语不再是默认语言,而是德语:
expected: "Project test customer - Project test name (Audit, Access for all, 2015-06-15).pdf"
got: "Project test customer - Project test name (Audit, Zugang für alle, 2015-06-15).pdf"
Run Code Online (Sandbox Code Playgroud)
可以看出,"人人享有"这一部分突然间是"Zugangfüralle".我搜索了一个解决方案,它似乎I18n.locale是一个全局对象,所以当它在规范中发生变化时,它会持续存在.
问题并不总是发生,但我可以在指定这样的种子时重现它:rspec --seed 51012.因此,在某些其他规范之前(或之后)执行规范似乎确实存在一些问题.
我有一个功能规范,测试是否可以更改语言环境,如下所示:
it 'offers contents in german' do
visit root_path(locale: :de)
expect(page).to have_content 'Willkommen'
end
Run Code Online (Sandbox Code Playgroud)
我怀疑这可能是有问题的规范,当它提前运行时,它会影响其他规格.
我希望我可以通过将规范中的语言设置回默认值来解决它,如下所示:
it 'offers contents in german' do
visit root_path(locale: :de)
expect(page).to have_content 'Willkommen'
I18n.locale = :en
end
Run Code Online (Sandbox Code Playgroud)
没工作,这个也没有:
it 'offers contents in german' do
visit root_path(locale: :de)
expect(page).to have_content …Run Code Online (Sandbox Code Playgroud) rspec-rails ×10
rspec ×6
rspec2 ×3
ruby ×3
bundler ×1
cancan ×1
capybara ×1
controller ×1
guard ×1
rails-i18n ×1
rake ×1
spork ×1
testing ×1