Tag: rspec

RSpec – 测试是否使用之前定义的块调用块

我最近问过如何在RSpec中测试是否调用了一个块,并且该问题的答案似乎在一个简单的情况下起作用。 问题是当块的初始化更复杂时。 然后它在before完成并在上下文中通过许多不同的测试重用,其中一个测试是否评估了块。 看例子: context “the node definition using block of code” do before do @n=node do # this block should be called end # some more complex setup concerning @n end it “should call the block” do # how to test it? end # here a bunch of other tests using @n end 在这种情况下,具有副作用改变局部变量值的解决方案不起作用。 […]

测试JavaScript重定向的麻烦

我正在使用Ruby on Rails 3.1.0和rspec-rails 2 gem。 我想测试一个JavaScript重定向,但我这样做有些麻烦。 在我的控制器文件中,我有: respond_to do |format| format.js { render :js => “window.location.replace(‘#{users_url}’);” } end 在我的spec文件中,我有: it “should redirect” do xhr :post, :create response.should redirect_to(users_url) end 如果我运行规范,我会得到以下内容: Failure/Error: response.should redirect_to(users_url) Expected response to be a , but was 如何实现规范代码以便正确测试JavaScript重定向? 解决方案的尝试 如果我使用 response.should render_template(“window.location.replace(‘#{users_url}’);”) 我明白了 Failure/Error: response.should render_template(“window.location.replace(‘#{users_url}’);”) expecting <"window.location.replace('http:///users’);”> but rendering […]

Rspec – RuntimeError:nil的被调用id,错误地为4

我现在正在为ItemsController create操作进行Rspec测试,但是我遇到了错误。 (有Item , Auction , Bid , User等型号) create方法如下 # POST /items # POST /items.json def create @item = Item.new(params[:item]) @item.user_id = current_user.id respond_to do |format| if @item.save format.html { redirect_to @item, notice: ‘Item was successfully created.’ } format.json { render json: @item, status: :created, location: @item } else format.html { render action: “new” […]

无法通过Post在Rspec中创建模型

我正在尝试在Rspec中创建一个用户: require “rails_helper” RSpec.describe UsersController, type: :controller do describe “POST #index” do it ‘should create a user’ do post :create, {name: “name1”, age: 25}.to_json, format: :json # …………. 但我每次都会收到validation错误: name can’t be blank, age can’t be blank UserController接受json: def create @user = User.new(params1) if @user.save render json: @user else render json: @user.errors, status: :unprocessable_entity end […]

如何使用以前版本的Rspec运行我的规范?

我一直在我的一台机器上学习RSpec教程,希望能够学到更多关于BDD和TDD的知识。 我的设置是使用Rails 2.2.2和Rspec 1.1.12 今晚我决定继续使用我的主机并将我的代码从便携式设备移到桌面上。 没有RSpec,我安装了gem。 。 。 sudo gem install rspec sudo gem install rspec-rails 纷争和诽谤! 安装了新版Rspec! 1.2.0! 而现在我的测试都失败了! 虽然我完全打算跟进并学习最新版本,但我真的想完成本教程的内容,而不必重新开始。 我想知道。 有没有办法安装并指定我想针对之前的Rspec,1.2.12运行我的代码?

Rspec和bundle exec

我遇到了rspec的性能问题。 如果我做rspec spec/* ,加载我所有的测试需要2分钟以上(我只有大约100次测试)。 但如果我bundle exec rspec spec/* ,则加载时间不到10秒。 我在这里发现这篇文章恰恰相反。 为什么’rspec spec’比’bundle exec spec’运行得更快,使用的资源更少 那里的任何专家都知道发生了什么事吗? ps这个项目是一个纯粹的ruby项目,所以这里没有rails魔法。

Capybara :: ElementNotFound仅适用于特拉维斯的所有规格

我正在使用selenium,capybara和rspec进行测试,我注意到所有测试都在本地传递,但是在travis上,所有测试都失败了,每个规范都出现此错误。 Capybara::ElementNotFound: 我不确定这里有什么问题,这是.travis.yml上的specs runner script: – xvfb-run bundle exec rspec spec/features/*.rb 对于spec_helper.rb这里是我的配置: RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.use_transactional_fixtures = false config.infer_base_class_for_anonymous_controllers = false1 config.filter_run_excluding :broken => true config.render_views Capybara.default_driver = :selenium Capybara.default_selector= :css Capybara.run_server = true Capybara.default_wait_time = 30

Nokogiri与Rspec

有没有办法在Rspec中使用Nokogiri? 特别是我试图从控制器动作获取响应并将其转换为Nokogiri对象page并运行Nokogiri特定的解析,如下所示: page.search(‘input[name=”some_name”]’).size.should == 1 我在哪里包括Nokogiri – 那将是spec_helper.rb吗? 如何将ActionController::TestResponse转换为Nokogiri对象? 或者是否可以通过使用普通的Rspec语法来运行上述类型的断言?

用于ActionDispatch路由的Rspec undefined方法’helpers’

我正在将旧的rails 3应用程序升级到rails 5,并在此过程中升级Rspec。 运行测试时,出现错误: NoMethodError Exception: undefined method `helpers’ for # 每当我在响应上调用be_success时。 见例子: it “renders ‘OK’ text when request is html” do get :ping expect(response).to be_success response.body.should == ‘OK’ end 我在运行此测试时遇到错误。 插入断点后,我看到每当调用be_success时都会抛出错误… (byebug) be_success *** NoMethodError Exception: undefined method `helpers’ for # Did you mean? helper_names 我在我的spec_helper文件中包含了config.include Rails.application.routes.url_helpers(使用rails路径路径时遇到了类似的问题),但它对此错误没有帮助。

Rspec / mocks引发未初始化的常量BasicObject :: RSpec

我刚刚安装了rspec和rspec-mocks,但我无法运行最简单的设置 irb(main):001:0> require ‘rspec’ => true irb(main):004:0> require ‘rspec/mocks’ => false irb(main):006:0> RSpec::Mocks::setup(Object.new) NameError: uninitialized constant BasicObject::RSpec from /usr/local/Cellar/ruby/1.9.1-p376/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.13.0/lib/rspec/mocks.rb:60:in `block in add_extensions’ from /usr/local/Cellar/ruby/1.9.1-p376/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.13.0/lib/rspec/mocks.rb:60:in `class_eval’ from /usr/local/Cellar/ruby/1.9.1-p376/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.13.0/lib/rspec/mocks.rb:60:in `add_extensions’ from /usr/local/Cellar/ruby/1.9.1-p376/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.13.0/lib/rspec/mocks.rb:11:in `setup’ from (irb):6 from /usr/local/Cellar/ruby/1.9.1-p376/bin/irb:12:in `’ 我想知道什么是错的,我需要加载一些其他的gem吗?