Tag: rspec

Rspec:在帮助测试中设置cookie

助手方法 # Determine if this is user’s first time def first_time? cookies[:first_time].nil? end 尝试过Rspec测试 it “returns true if the cookie is set” do cookies[:first_time] = “something” helper.first_time?().should be(true) end 错误: undefined method `cookies’ for nil:NilClass 我读过有关Rspec和cookies的所有内容都与控制器有关。 在Rspec帮助器测试中获取/设置cookie的任何方法? (Rspec / Rspec-rails 2.5,Rails 3.0.4) 谢谢!! 更新: 找到了关于如何设置cookie的答案,所以我会留在这里供其他人参考。 我正在寻找的那件作品: helper.request.cookies[:awesome] = “something” 仍然不知道如何获取cookies……

Capybara webkit无效响应错误,如何调试?

我正在尝试为网页编写请求规范。 此页面正在开发中,没有错误。 但是在capybara webkit中运行后,在尝试提交表单后出现此错误: Failure/Error: Unable to find matching line from backtrace Capybara::Driver::Webkit::WebkitInvalidResponseError: Unable to load URL: https://127.0.0.1:33416/sign_in 为了找出原因,我开始在页面上剥离标记和JavaScript。 到了具有普通提交按钮的空表单的程度。 我仍然得到上述错误! 该测试现在字面上: it “should be able create a new foo”, :js, :focus do visit new_foo_path find(‘#submit’).click end 但是,如果我删除:js选项,此测试确实有效: it “should be able create a new foo”, :focus do visit new_foo_path find(‘#submit’).click end Javascript测试在此应用程序的其他页面中有效… 这对我没有意义。 有没有人有任何建议如何从这里调试? […]

使用’。’来路由路由和参数 在他们中

我使用rails来保护对只需要为Web应用程序的某些用户提供服务的文件的访问权限。 为此,我有一个控制器方法,它接受有关他们想要访问的文件的信息,检查他们的授权,然后如果他们被授权使用x-sendfile将它发送给他们。 除了一个障碍之外,这个概念工作正常:如果他们请求一个资源。 在其中我的路由不知道处理它。 在我的路线文件中我有: match ‘atb_resources/:guid/:resource’ => ‘atb_resources#show’, :as => :get_atb_resource, :via => :get 然后如果我在我的规范中尝试这个: get ‘show’, :guid => ‘some_guid’, :resource => ‘blah.txt’ 规范失败了: Failure/Error: get ‘show’, :guid => @atb.guid, :resource => ‘blah.txt’ ActionController::RoutingError: No route matches {:guid=>”ABCDEFG5″, :resource=>”blah.txt”, :controller=>”atb_resources”, :action=>”show”} 但这没关系: get ‘show’, :guid => ‘some_guid’, :resource => ‘blahDOTtxt’ 我假设问题出在我的路由上,但我真的不明白周期如何影响路由。 有任何想法吗?

rspec&设计测试助手

根据设计维基的这个 ,我应该能够在我的控制器测试中使用login_user帮助器方法。 因此我在spec目录中有以下内容: #spec_helper.rb … Dir[Rails.root.join(“spec/support/**/*.rb”)].each {|f| require f} RSpec.configure do |config| config.include Devise::TestHelpers, :type => :controller config.extend ControllerMacros, :type => :controller … 和 #support/controller_macros.rb module ControllerMacros def login_user before(:each) do @request.env[“devise.mapping”] = Devise.mappings[:user] @user = Factory.create(:user) sign_in @user end end end 但是调用帮助程序不起作用: #requests/some_spec.rb require ‘spec_helper’ describe “GET /guides/edit” do login_user end 有人能指出我出错的地方。 测试套件就是这样工作的。 我得到一个未定义的局部变量或方法消息,所以我猜这个模块没有被包含。 […]

无法使用rspec存储辅助方法

我试图在我的控制器中定义的助手上存根方法。 例如: class ApplicationController < ActionController::Base def current_user @current_user ||= authenticated_user_method end helper_method :current_user end module SomeHelper def do_something current_user.call_a_method end end 在我的Rspec中: describe SomeHelper it “why cant i stub a helper method?!” do helper.stub!(:current_user).and_return(@user) helper.respond_to?(:current_user).should be_true # Fails helper.do_something # Fails ‘no method current_user’ end end 在spec/support/authentication.rb module RspecAuthentication def sign_in(user) controller.stub!(:current_user).and_return(user) controller.stub!(:authenticate!).and_return(true) helper.stub(:current_user).and_return(user) […]

RSpec找不到我的命名路线

我有一个莫名其妙的困难时间让我的命名路线在我的rspec测试中工作。 这就是我所看到的: 1) Home GET / works! Failure/Error: get root_path NameError: undefined local variable or method `root_path’ for # # ./spec/requests/home_spec.rb:6:in `block (3 levels) in ‘ 2) Home GET / shows products! Failure/Error: get products_path NameError: undefined local variable or method `products_path’ for # # ./spec/requests/home_spec.rb:10:in `block (3 levels) in ‘ 规格/请求/ home_spec.rb require ‘spec_helper’ […]

FactoryGirl关联模型故障:“SystemStackError:堆栈级别太深”

我正在使用Ruby on Rails 3.0.9,RSpec-rails 2和FactoryGirl。 我试图陈述一个工厂协会模型,但我遇到了麻烦。 我有一个factories/user.rb文件,如下所示: FactoryGirl.define do factory :user, :class => User do attribute_1 attribute_2 … association :account, :factory => :users_account, :method => :build, :email => ‘foo@bar.com’ end end 和一个factories/users/account.rb文件如下: FactoryGirl.define do factory :users_account, :class => Users::Account do sequence(:email) {|n| “foo#{n}@bar.com” } … end end 上面的示例在我的spec文件中按预期工作,但是如果在factory :users_account语句我添加了association :user代码所以要有 FactoryGirl.define do factory :users_account, :class […]

如何测试after_sign_in_path_for(资源)?

我已经在我的Rails应用程序上设置了身份validation和注册设置。 我正在使用after_sign_in_path_for()来根据各种场景在用户after_sign_in_path_for()时自定义重定向。 我问的是如何测试这种方法? 它很难被隔离,因为当用户进入时,Devise会自动调用它。我想做这样的事情: describe ApplicationController do describe “after_sign_in_path_for” do before :each do @user = Factory :user @listing = Factory :listing sign_in @user end describe “with listing_id on the session” do before :each do session[:listing_id] = @listing.id end describe “and a user in one team” do it “should save the listing from the session” do […]

使用open_id_authentication插件时,如何在RSpec用户故事/ Cucumber中伪造OpenID登录

我正在尝试编写一个Cucumber场景,要求我有一个登录用户 – 这通常很简单,但我只使用OpenID身份validation(认证插件的简化)。 然而,在深入了解open_id_authentication插件后,我不确定如何在Cucumber中实现这一点。

ActiveRecord :: Base的未定义方法`mass_assignment_sanitizer =’:Class(NoMethodError)

我正在使用Michael Hartl的精彩Ruby on Rails教程学习Rails。 我在3.2.2节(测试驱动开发)中,我需要运行以下命令来运行我的Rails项目的rspec测试: bundle exec rspec spec/ 但它不起作用。 相反,我得到这个错误: /Users/mh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/ activerecord-3.1.3/lib/active_record/base.rb:1088:in `method_missing’: undefined method `mass_assignment_sanitizer=’ for ActiveRecord::Base:Class (NoMethodError) 我已经尝试重新安装rspec并更改我的Gemfile ,但没有任何安抚未定义的方法错误!