Tag: rspec

rspec,未知属性问题

我正在通过(优秀)railstutorial.org网站上有一个关于rspec的基本问题。 当我在新用户模型上运行以下测试时,我收到“未知属性:用户名”消息和失败的测试。 before(:each) do @attr = { :lname_e => “User”, :fname_e => “Test”, :email => “user@example.com”, :username => “testUser” } end it “should create a new instance given valid attributes” do User.create!(@attr) end 错误语法是 Failures: 1) User should create a new instance given valid attributes Failure/Error: User.create!(@attr) unknown attribute: username # ./spec/models/user_spec.rb:11:in `block (2 levels) […]

使用rspec进行集成测试并设计sign_in env

我正在使用设计配置使用omniauth facebook登录集成。 从我的spec/request测试中调用sign_in方法时,我得到: undefined method `env’ for nil:NilClass 规格: describe FacebookController do include Devise::TestHelpers it “should display facebook logged in status” do @user = User.create(:id => “123”, :token => “token”) sign_in @user visit facebook_path end end

我可以从安装它的真实应用程序运行Rails引擎的规范吗?

我有一个Rails引擎,旨在为我们的大型项目提供一些模型和控制器。 引擎的规格相当不错,在引擎的虚拟应用程序中使用了一堆模拟和一些全尺寸模型和控制器,以确保引擎正在做它应该做的事情并使用更大的应用程序。 但是,即使所有测试都通过,我也经常在更大的应用程序中更新引擎时发现损坏的行为。 如果我的测试通过但是行为被破坏了,那么测试显然有些问题,但是什么呢? 我嘲笑太多,还是不够? 为了让我更接近解决这个问题,我希望能够在整个应用程序中运行引擎的测试。 这似乎应该是可能的,但我不完全了解rspec如何处理。 (这与这个问题有关但不完全相同;我不是试图从一个命令运行所有规范,只是为了在完整的应用程序环境中运行引擎的规范。 这似乎也是相关的。实际上,我’我读过用rspec和rails-engines标记的每一个问题 – 这些问题并不多 – 而且它们都不是我需要的,或者没有答案。)

来自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 […]

如何在RSpec中使用HTTP状态码符号?

我在控制器的代码中使用HTTP状态代码符号 ,例如: render json: { auth_token: user.authentication_token, user: user }, status: :created 要么 render json: { errors: [“Missing parameter.”] }, success: false, status: :unprocessable_entity 在我的请求规范的代码我也想使用符号: post user_session_path, email: @user.email, password: @user.password expect(last_response.status).to eq(201) … expect(last_response.status).to eq(422) 但是,我使用符号而不是整数的每个测试都会失败: Failure/Error: expect(last_response.status).to eq(:created) expected: :created got: 201 (compared using ==) 以下是Rack中最新的HTTP状态代码符号列表。

Rspec,Cucumber:最佳速度数据库清洁策略

我想提高测试的速度。 我应该使用use_transactional_fixtures还是使用database_cleaner gem? 哪种database_cleaner策略最好? 我注意到从迁移后:truncation到:transaction我的800多个例子运行速度快了4倍! 当我使用database_cleaner :transaction时,我应该关闭use_transactional_fixtures吗? Rack_test的最佳策略是:transaction吗? 在使用selenium或akephalos时,从:transaction到:truncation的最佳实践是什么? PS Mysql,Rails 3,Rspec2,Cucumber PPS我知道spork和parallel_test并使用它们。 但他们是偏离主义的。 例如,Spork在整个套件运行中节省大约15-20秒,但从:transaction更改为:truncation显着地将运行时间从3.5增加到13.5分钟(差异10分钟)。

rspec测试控制器post将我的参数从符号改为字符串并破坏我的测试

在我的控制器规范中,我这样做: it “should create new message” do Client.should_receive(:create).with({:title => ‘Mr’}) post ‘create’ , :client => {:title => “Mr” } end ……在我的控制器里,我正在…… def create client = Client.create(params[:client]) end 但是,这失败并出现以下错误消息: expected: ({:title=>”Mr”}) got: ({“title”=>”Mr”}) 我想知道为什么会这样,以及如何让它发挥作用

如果rspec测试失败,请启动ruby调试器

通常,当测试失败时,我花了很长时间试图找出导致测试失败的原因。 如果RSpec在测试失败时启动Ruby调试器会很有用,这样我就可以立即检查局部变量以深入了解原因。 我正在使用的解决方案现在看起来像这样: # withing some test debugger unless some_variable.nil? expect(some_variable).to be_nil 但是,这种方法很麻烦,因为我首先等待测试失败,然后添加调试器行,修复问题然后必须删除调试器行,而我希望它的工作更像gdb ,它有能力启动当遇到exception时,无需使用debugger语句来编写代码库。 编辑:我试过普利茅斯。 它对我来说不够可靠。 此外,开发历史似乎表明它不是一个非常受支持的gem,所以我宁愿不依赖它。 更新 :我尝试了pry-rescue并发现它很整洁。 但是,我经常使用宙斯,并且想知道是否有办法让它与pry-rescue 。

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的每个测试块执行此操作?

尝试使用rspec,但是得到了一个错误,即rspec-core 2.2.1已被激活,但我的Gemfile需要rspec-core 2.1.0

我更新了我的gem。 我已经创建了一个示例Rails应用程序,并在我的Gemfile中有以下内容: source ‘http://rubygems.org’ gem ‘rails’, ‘3.0.3’ gem ‘sqlite3-ruby’, :require => ‘sqlite3’ group :development do gem ‘rspec-rails’ end group :test do gem ‘rspec’ gem ‘webrat’, ‘0.7.1’ end 但是,当我运行’rspec spec /’时,我收到以下消息: /home/jeff/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.7/lib/bundler/runtime.rb:27:in `block in setup’: You have already activated rspec-core 2.2.1, but your Gemfile requires rspec-core 2.1.0. Consider using bundle exec. (Gem::LoadError)