Tag: rspec

NoMethodError:未定义的方法`call’

运行以下测试套件时: require ‘spec_helper’ describe User do before { @user = User.(name: “Example User”, email: “user@example.com” } subject { @user } it { should respond_to(:name) } it { should respond_to(:email) } end 我收到此错误: Failure/Error: before { @user = User.(name: “Example User”, email: “user@example.com”) } NoMethodError: undefined method `call’ for # # ./spec/models/user_spec.rb:15:in `block (2 levels) in […]

Rubyunit testing技术,Mocking和Stubbing

我被招募为SW Dev,我正在尝试使用RSPEC和RR进行unit testing,但是很难决定特定策略,主要是因为我被分配给已经编码的unit testing书面。 考虑以下代码,它是一个名为method1的大方法的一部分: if ([“5234541252”, “6236253223”].include?(self.id)) self.DoCheck logs.add ‘Doing check’, “id = #{self.id}” return end 这部分方法的相关unit testing将是这样的: “should do check only if id = 5234541252 or 6236253223” 但我遇到了几个问题,基本上涉及最佳实践,例如: 如何使用RR和RSPEC检查是否已从“method1”中调用DoCheck? 我尝试过使用dont_allow(Object).DoCheck但是它不起作用。 describe :do_route do it “should do check only if id = 5234541252 or 6236253223” do user = Factory(:User) user.id = “5234541252” dont_allow(user).DoCheck user.method1 […]

RSpec 1.3.3 Rails 2.3.9 Webrat 0.7.3“未定义方法`assign`”

我的规格 require ‘spec_helper’ describe ‘user_sessions/new.html.erb’ do let (:user_session) { mock_model(UserSession).as_null_object } before do assign(:user_session, user_session) end it ‘should have the sign in header’ do render rendered.should contain(‘Sign in’) end end 抛出 1) NoMethodError in ‘user_sessions/new.html.erb should have the sign in header’ undefined method `assign’ for # gem: group :test, :cucumber do gem “cucumber-rails”, “0.3.2” gem […]

Rspec测试失败,当用户被授权访问他拥有的交易时,用户被重定向

在我的应用程序中,感谢Cancan,我可以让客户访问他自己的交易。 当我使用浏览器“手动”尝试它时,它可以正常工作, 但我在实现rspec测试时失败了 。 客户无法访问其他客户的交易,只能访问自己的交易(管理员通过Active Admin界面授予他访问权限)。 这就像我没有让rspec明白我为测试创建的客户(通过FactoryGirl)应该被允许/与我为测试创建的交易相关联(再次通过FactoryGirl)。 测试:/spec/controllers/deals_controller_spec.rb require ‘spec_helper’ require “cancan/matchers” describe DealsController do context “As signed-in CUSTOMER” do before do @customer = FactoryGirl.create(:customer) #the factory builds a basic customer ie with ‘prospect role’ attributed by default @deal = FactoryGirl.create(:deal, :customers => [@customer]) # in array as a deal has_many customers sign_in_customer @customer end […]

与rspec-rails 3.0.1和shoulda的测试关联不起作用

目前,使用rspec-rails(2.14.2),我使用shoulda(3.5.0)gem测试模型规范中的关联,如下所示: # app/models/user.rb class User < ActiveRecord::Base belongs_to :school end # spec/models/user_spec.rb describe User do it { should belong_to :school } end 经过一些研究,我试图让与关联相关的断言起作用(它们似乎都失败了)。 错误信息: 1) User Failure/Error: it { is_expected.to belong_to :school } NoMethodError: undefined method `belong_to’ for # # ./spec/models/user.rb:4:in `block (2 levels) in ‘ 所以我的问题是: 你可以测试没有shoulda gem的协会吗? 根据我在“expect”语法中看到的内容,这似乎不可行。 对于每个人来说,shoulda gem是否会与rspec 3.0.1打破? 有解决方法吗?

Aruba不与RSpec 3合作?

我正在尝试测试一个ruby命令行脚本,但在RSpec 3中运行使用无黄瓜Aruba的测试时遇到了麻烦。 一些奇怪的错误,一些明显的错误。 如奇怪的: 1) test does something Failure/Error: run_simple(“cli_test.rb -h”, true) ArgumentError: wrong number of arguments (2 for 1) # …/.gem/ruby/2.2.0/gems/aruba-0.6.2/lib/aruba/api.rb:632:in `assert_exit_status’ # …/.gem/ruby/2.2.0/gems/aruba-0.6.2/lib/aruba/api.rb:750:in `run_simple’ # ./spec/cli_test_spec.rb:17:in `block (2 levels) in ‘ 查看代码,我甚至无法看到是什么触发了这一点。 试图使用Pry或Pry-byebug来检查错误的2个args也没有工作(一大堆其他错误)。 然后,例如明显: 1) test does something Failure/Error: check_file_presence([“bin/cli_test.rb”], true) only the `receive` or `receive_messages` matchers are supported with `expect(…).to`, but you […]

ruby rspec不会使用simplecov

我安装了simplecov gem并添加了 require ‘simplecov’ SimpleCov.start 到spec_helper.rb文件,现在,如果我在some_file_spec.rb文件中包含spec_helper.rb并尝试运行它我得到这个 c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuratio n.rb:116:in `require’: no such file to load — rspec/core/mocking/with_rspec (Lo adError) from c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core /configuration.rb:116:in `mock_framework’ from c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core /configuration.rb:376:in `configure_mock_framework’ from c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core /command_line.rb:19:in `run’ from c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core /runner.rb:55:in `run_in_process’ from c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core /runner.rb:46:in `run’ from c:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core /runner.rb:10:in `block in autorun’ Coverage report generated for c:/Ruby192/bin/rspec player_spec.rb to c:/Users/Ko Le/Documents/NetBeansProjects/CHCGame/spec/coverage. 0.0% covered. […]

ruby on rails rspec错误

我正在通过“按示例学习Rails”一书,并且我正在尝试运行测试。 出于某种原因,我无法让rspec正常工作。 如果我按照他的指示运行rspec spec/命令,我会收到以下错误: $ rspec spec/ /home/desktop/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.21/lib/bundler/runtime.rb:31:in `block in setup’: You have already activated rspec-core 2.7.1, but your Gemfile requires rspec-core 2.6.4. Using bundle exec may solve this. (Gem::LoadError) 奇怪的是我的Gemfile没有指定版本 – group :development do gem ‘rspec-rails’ end group :test do gem ‘rspec’ gem ‘webrat’ end 如果我按照错误消息的建议并使用bundle exec rspec spec/然后前两个测试通过 – 但是我们在教程中构建的新“about”页面失败并出现以下错误,尽管据我所知我构建的页面(以及控制器操作等)完全符合它们的要求: Failures: 1) PagesController […]

在某些RSpec rails请求示例中测试HTTP状态代码,但是对于其他RSPro rails请求示例

在使用rspec-rails测试的Rails 4.2.0应用程序中,我提供了一个带有类似REST资源的JSON Web API,其强制属性为mand_attr 。 我想测试一下,当POST请求中缺少该属性时,此API会回答HTTP代码400( BAD REQUEST )。 (请参阅第二个示例。)我的控制器尝试通过抛出ActionController::ParameterMissing来生成此HTTP代码,如下面的第一个RSpec示例所示。 在其他 RSpec示例中,我希望通过示例(如果它们是预期的)挽救引发的exception或者命中测试运行器,因此它们会显示给开发人员(如果错误是意外的),因此我不想要去除 # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = false 来自config/environments/test.rb 我的计划是在请求规范中包含以下内容: describe ‘POST’ do let(:perform_request) { post ‘/my/api/my_ressource’, request_body, request_header } let(:request_header) { { ‘CONTENT_TYPE’ => ‘application/json’ } } context ‘without mandatory attribute’ do let(:request_body) do {}.to_json end it ‘raises a […]

RSpec无法找到我的控制器未初始化常量

我的rails应用程序最近从Rails 3转移到Rails 4,我一直试图在各个规格(控制器,模型等)上运行rspec,而且它似乎有一个实际定位对象的问题。 例如,当我尝试运行以下代码时,它会出现未初始化的常量错误。 这似乎发生在多个控制器上。 我已经尝试删除rails_helper.rb和spec_helper.rb并运行rails生成rspec:install但它似乎无法解决错误。 为什么找不到控制器呢? 我正在尝试执行什么 -> rspec spec/controllers/activity_controller_spec.rb /Users/osx_user/rails_projects/tealeaf_rails/ltbweb/spec/controllers/activity_controller_spec.rb:4:in `’: uninitialized constant ActivitiesController (NameError) rails_helper.rb # This file is copied to spec/ when you run ‘rails generate rspec:install’ ENV[“RAILS_ENV”] ||= ‘test’ require ‘spec_helper’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ # Add additional requires below this line. Rails is not loaded until this point! […]