Tag: rspec2

来自csv.read模拟文件的rspec测试结果

我正在使用ruby 1.9而我正在尝试做BDD。 我的第一个测试“应该在csv中读取”,但是第二个我需要模拟文件对象的测试却没有。 这是我的型号规格: require ‘spec_helper’ describe Person do describe “Importing data” do let(:person) { Person.new } let(:data) { “title\tsurname\tfirstname\t\rtitle2\tsurname2\tfirstname2\t\r”} let(:result) {[[“title”,”surname”,”firstname”],[“title2″,”surname2″,”firstname2”]] } it “should read in the csv” do CSV.should_receive(:read). with(“filename”, :row_sep => “\r”, :col_sep => “\t”) person.import(“filename”) end it “should have result” do filename = mock(File, :exists? => true, :read => data) person.import(filename).should […]

Rails 3和Rspec 2关闭各个测试的事务夹具

我正在将我的应用程序升级到Rails 3.我开始在Rails 3中使用Rspec 2.我需要为我的一些rspec测试关闭事务装置。 之前我在我的模型规范中使用了以下代码 before(:all) do ActiveSupport::TestCase.use_transactional_fixtures = false end after(:all) do ActiveSupport::TestCase.use_transactional_fixtures = true clean_engine_database end 那现在给了我错误: Failure/Error: ActiveSupport::TestCase.use_transactional_fixtures = false undefined method `use_transactional_fixtures=’ for ActiveSupport::TestCase:Class 有没有办法在Rails 3中使用Rspec 2的每个测试块执行此操作?

RoR:测试使用http令牌身份validation的操作

我正在尝试测试在前置filter中使用http令牌身份validation的控制器。 我的问题是,它使用curl传递令牌是可行的,但在我的测试中它总是失败(我使用的是rspec btw)。 尝试了一个简单的测试,看看是否正在传递令牌,但似乎它没有这样做。 我错过了什么来让测试实际将令牌传递给控制器​​? 这是我之前的filter: def restrict_access authenticate_or_request_with_http_token do |token, options| api_key = ApiKey.find_by_access_token(token) @user = api_key.user unless api_key.nil? @token = token #set just for the sake of testing !api_key.nil? end end 这是我的测试: it “passes the token” do get :new, nil, :authorization => ActionController::HttpAuthentication::Token.encode_credentials(“test_access1”) assigns(:token).should be “test_access1” end

除了在Rails测试中使用Capybara和RSpec之外,Steak还能添加什么?

我正在努力了解牛排的必要性。 我得到它像Cucumber,除了你可以使用纯ruby而不是像Cucumber一样将你的英语语言规范映射到ruby,但是它说它主要是在RSpec DSL周围增加一个包装,并让你使用它 取自: http : //jeffkreeftmeijer.com/2010/steak-because-cucumber-is-for-vegetarians/ module Spec::Example::ExampleGroupMethods alias scenario example alias background before end module Spec::DSL::Main alias feature describe end 这就是全部? 我从例子中可以看出,你仍然在使用Capybara和RSpec匹配器进行繁重的工作。那么为什么不直接使用Capybara和RSpec而不是在它上面添加像Steak这样的复杂function呢? 英语语言接受规范描述是唯一的价值主张,还是我错过了其他什么? 谢谢

如何测试RSpec中的attr_accessible字段

因此,我们通过Rails 3.2应用程序在许多字段上设置了attr_accessible和attr_protected 。 目前我们确实没有测试以确保这些字段受到保护。 所以我决定谷歌一些答案,偶然发现这个解决方案: RSpec::Matchers.define :be_accessible do |attribute| match do |response| response.send(“#{attribute}=”, :foo) response.send(“#{attribute}”).eql? :foo end description { “be accessible :#{attribute}” } failure_message_for_should { “:#{attribute} should be accessible” } failure_message_for_should_not { “:#{attribute} should not be accessible” } end 但是这个解决方案只测试方法是否响应。 我需要的是一种方法,让我测试属性可以和不能被大量分配。 老实说,我喜欢这种语法 it { should_not be_accessible :field_name } it { should be_accessible :some_field } 有没有人有更好的解决方案来解决这个问题?

在发出请求之前,Rspec 2.7访问控制器会话

我正在使用Rspec测试我的控制器,在向路径发出请求之前,我似乎无法设置当前控制器的会话变量。 例如,这有效: describe “GET /controller/path” do it “if not matching CRSF should display message” do get controller_path request.session[:state] = “12334” end end 这不起作用(我得到一个错误,说session不是Nil类的方法): describe “GET /controller/path” do it “if not matching CRSF should display message” do request.session[:state] = “12334” get controller_path end end 有任何想法吗?

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……

无法使用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) […]

如何测试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 […]

如何在Rails 3.1可安装引擎中测试路由

我正在尝试为可安装的rails 3.1引擎编写一些路由规范。 我有工作模型和控制器规格,但我无法弄清楚如何指定路线。 对于示例引擎’testy’,我尝试的每个方法都以相同的错误结束: ActionController::RoutingError: No route matches “/testy” 我已经尝试了Rspec和Test :: Unit语法(spec / routing / index_routing_spec.rb): describe “test controller routing” do it “Routs the root to the test controller’s index action” do { :get => ‘/testy/’ }.should route_to(:controller => ‘test’, :action => ‘index’) end it “tries the same thing using Test::Unit syntax” do assert_routing({:method => […]