Tag: rspec

如何在请求规范中存储ApplicationController方法

我需要在Rspec / capybara请求规范中存根current_user方法的响应。 该方法在ApplicationController定义,并使用helper_method。 该方法应该简单地返回一个用户ID。 在testing中,我希望这种方法每次都返回相同的用户ID。 或者,我可以通过在spec(这是current_user返回的内容)中设置session[:user_id]来解决我的问题…但是这似乎也不起作用。 这两者中的哪一个可能? 编辑: 这是我得到的(它不工作,它只是运行正常的current_user方法)。 require 'spec_helper' describe "Login" do before(:each) do ApplicationController.stub(:current_user).and_return(User.first) end it "logs in" do visit '/' page.should have_content("Hey there user!") end end 也不工作: require 'spec_helper' describe "Login" do before(:each) do @mock_controller = mock("ApplicationController") @mock_controller.stub(:current_user).and_return(User.first) end it "logs in" do visit '/' page.should have_content("Hey there user!") end […]

如何testingparams传递到控制器在轨3,使用rspec?

我们的代码: describe "GET show" do it "assigns the requested subcategory as @subcategory" do subcategory = Subcategory.create! valid_attributes get :show, :id => subcategory.id.to_s assigns(:subcategory).should eq(subcategory) end it "has a sort parameter in the url" do subcategory = Subcategory.create! valid_attributes get :show, {:id => subcategory.id.to_s, :params => {:sort => 'title'}} helper.params[:sort].should_not be_nil end end 我收到以下错误信息: 1) SubcategoriesController […]

Shoulda / RSpec匹配器 – 条件validation

在我的代码中,我有以下validation与Shoulda matchers,它工作正常: it { should validate_presence_of(:name) } 在我的模型中,我已经添加了条件来validation: validates_presence_of :name, :if => eligible? 有没有可能在validation中反映出来? 我已经尝试查看应用程序匹配器的文档 ,但一直无法find解决scheme。 非常感谢!

使用Rspec,我如何在Rails 3.0.11中testing我的控制器的JSON格式?

我search了网页,但是,唉,我似乎无法让Rspec正确发送内容types,所以我可以testing我的JSON API。 我正在使用RABL gem模板,Rails 3.0.11和Ruby 1.9.2-p180。 我的curl输出,工作正常(应该是一个401,我知道): mrsnuggles:tmp gaahrdner$ curl -i -H "Accept: application/json" -X POST -d @bleh http://localhost:3000/applications HTTP/1.1 403 Forbidden Content-Type: application/json; charset=utf-8 Cache-Control: no-cache X-Ua-Compatible: IE=Edge X-Runtime: 0.561638 Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18) Date: Tue, 06 Mar 2012 01:10:51 GMT Content-Length: 74 Connection: Keep-Alive Set-Cookie: _session_id=8e8b73b5a6e5c95447aab13dafd59993; path=/; HttpOnly {"status":"error","message":"You are not authorized to access […]

Ruby Rspec:testing实例variables而不添加访问者到源

我试图testing下面的方法: def unprocess_move(board, move) if move[0].instance_of?(Array) multi_move = @multi_move.pop(2).reverse multi_move.each do |single_move| unapply_move(board, single_move) end else board = unapply_move(board, move) end board end 我想为@multi_move设置状态,但是我不想添加一个访问器来进行testing。 没有访问器有没有办法做到这一点? 谢谢。

Ruby on Rails:从test_unit切换到rspec

我正在通过一个build议使用rspec的教程,但是我已经经历了很多默认的rails安装。 我真的不想重做安装。 无论如何,当我跑步 $ rails g integration_test named 我明白了 invoke test_unit create test/integration/named_test.rb 当我运行bundle ,列出了各种rspecgem,但test_unit不是。 本教程似乎有轨道调用rspec而不是test_unit没有做任何额外的事情。 如何获得使用rspec和集成testing生成器命令的rails?

Rspectestingredirect_to:回来

你如何testingredirect_to :back rspec? 我明白了 ActionController::RedirectBackError : 在这个动作的请求中没有设置HTTP_REFERER ,所以redirect_to :back无法成功调用。 如果这是一个testing,请确保指定request.env["HTTP_REFERER"] 。 如何在我的testing中设置HTTP_REFERER ?

在RSpec中如何说“any_instance”“should_receive”任意次数

我有一个导入控制器导入几个CSV文件到我的数据库的多个logging。 我想在RSpec中进行testing,如果使用RSpec实际保存logging: <Model>.any_instance.should_receive(:save).at_least(:once) 不过,我得到的错误说: The message 'save' was received by <model instance> but has already been received by <another model instance> 控制器的一个人为的例子: rows = CSV.parse(uploaded_file.tempfile, col_sep: "|") ActiveRecord::Base.transaction do rows.each do |row| mutation = Mutation.new row.each_with_index do |value, index| Mutation.send("#{attribute_order[index]}=", value) end mutation.save end 有没有可能使用RSpec来testing这个,或者是否有任何解决方法?

Rails / Rspec Maketesting通过http基本authentication

这里我的应用程序控制器文件(application_controller.rb)中的http基本身份validation before_filter :authenticate protected def authenticate authenticate_or_request_with_http_basic do |username, password| username == "username" && password == "password" end end 和我的家庭控制器的索引行动的默认testing(spec / controllers / home_controller_spec.rb) require 'spec_helper' describe HomeController do describe "GET 'index'" do it "should be successful" do get 'index' response.should be_success end end 由于validation方法,testing不会运行。 我可以评论“before_filter:authenticate”来运行它们,但我想知道是否有办法让它们与方法一起工作。 谢谢!

RSpec文档…它在哪里隐藏?

有一个可怕的时间findRSpec-2 (和rspec-rails )文档。 具体而言,我想查找所有可用的匹配器…但是如何? 我试过了: RSpec.info :过时 – RSpec 1.x rdoc.info :无论如何,对我来说似乎是无用的。 (不完整?) RSpec-Relish :令人困惑。 不完整? 如何查找可用的Matchers? GitHub Wiki :分散的信息。 无组织的。 不完整? 是的,我知道这本书 …但是,严重的是,这不是在网上? 甚至尝试过rake doc:plugins但没有奏效。 太绿知道为什么… 显然在rspec.rubyforge.org上曾经有一些体面的1.x文档。 但他们似乎早已不复存在 这些文档在哪里隐藏?