标签: rspec

rspec希望收到不工作

我有以下类结构:

class A
  def process(processor)
    processor.process_x
  end
end

class Processor
  def process_x
  end
end
Run Code Online (Sandbox Code Playgroud)

以及以下测试:

context "when process is called" do
  object_a = A.new
  processor = Processor.new

  it "calls processor.process_x" do
    object_a.process(processor)
    expect(processor).to receive(:process_x)
  end
end
Run Code Online (Sandbox Code Playgroud)

但测试仍然失败.我甚至放了一个byebug,看到处理器的process_x方法被调用

我该怎么解决这个问题?

rspec ruby-on-rails

0
推荐指数
1
解决办法
577
查看次数

使用ruby脚本的Rspec错误:"无法加载此类文件 - ./components/tasks.rb"(ruby app not rails)

我正在尝试构建我的Rspec测试测试来测试我正在构建的ruby应用程序.我知道我应该先建立,但稍后进行测试.代码确实100%工作我只是在让Rspec查看我的代码时遇到问题.

github上的完整代码:https: //github.com/EsdrasEtrenne/tictactoe

到目前为止,我使用rspec运行的唯一文件是ruby/spec/game_spec.rb

game_spec.rb文件如下所示:

require_relative "../tictactoe"
Rspec.describe Tasks do
  before(:each)do
    @game = Tictactoe::Game.new
  end

  it "has a working method called play" do
    expect{@game.play}.to output("WELCOME! To the unbeatable Tic-tac-toe").to_stdout
  end
end
Run Code Online (Sandbox Code Playgroud)

它需要tictactoe作为亲戚:

require "./components/tasks.rb"
require "./components/board.rb"
require "./components/player.rb"
require "./components/player_types/computer.rb"
require "./components/player_types/human.rb"

module Tictactoe
  class Game
    attr_reader :board, :player, :opponent, :tasks

    def initialize
      @board = Board.new
      @tasks = Tasks.new
    end

    def play
      @tasks.greet_players
      @player, @opponent = @tasks.get_order
      current_player, current_opponent = @player, @opponent
      @tasks.print_board(@board)

      until @board.game_is_over || @board.tie
        @tasks.tell_turn(current_player) …
Run Code Online (Sandbox Code Playgroud)

ruby rspec

0
推荐指数
1
解决办法
6361
查看次数

如何按升序排列值列表?

以下是数组中的值列表:

[463, 246, 216, 194, 154, 152, 147, 140, 129, 128, 123, 118, 118, 102, 102, 101, 97, 96, 93, 85]
Run Code Online (Sandbox Code Playgroud)

如何通过RSpec确保/断言数组列表是按升序排列的?

ruby rspec

0
推荐指数
1
解决办法
504
查看次数

在功能测试中使用位置参数已被弃用

我运行rspec时收到此警告: -

DEPRECATION WARNING: Using positional arguments in functional tests has been deprecated,
in favor of keyword arguments, and will be removed in Rails 5.1.

Deprecated style:
get :show, { id: 1 }, nil, { notice: "This is a flash message" }

New keyword style:
get :show, params: { id: 1 }, flash: { notice: "This is a flash message" },
  session: nil # Can safely be omitted.
 (called from block (4 levels) in <top (required)> at /home/user/organization/fooobarr/spec/controllers/contacts_controller_spec.rb:13)
Run Code Online (Sandbox Code Playgroud)

这是我的控制器规格: - …

rspec ruby-on-rails

0
推荐指数
3
解决办法
1221
查看次数

无法在无头模式下使用firefox,Capybara和Docker运行selenium

我正在尝试使用Selenium和Firefox与Capybara进行测试.当我运行测试时,它会对" http://127.0.0.1:4444:/sessions " 发出POST请求,它会启动一个服务器geckodriver并且我有一个超时错误.我尝试通过执行以下命令手动执行此操作:

curl -d '{"desiredCapabilities": {"browserName": "firefox", "version": "", "platform": "ANY", "javascriptEnabled": 1, "cssSelectorsEnabled": 1, "takesScreenshot": 1, "nativeEvents": 0, "rotatable": 0, "unexpectedAlertBehaviour": "ignore", "moz:firefoxOptions": {"args": ["-headless"]}}, "capabilities": {"firstMatch": [{"browserName": "firefox", "moz:firefoxOptions": {}}]}}' http://127.0.0.1:4444/session
Run Code Online (Sandbox Code Playgroud)

它失败了,我有这样的信息:Error: GDK_BACKEND does not match available displays.当我xvfb-run geckodriver开始工作时,它可以工作,但我必须找到一种方法,用RSpec在无头模式下启动geckodriver.

我在docker env上,所以我没有安装xserver.

我能做什么?

编辑

我有 :

Firefox 52.4
Geckodriver 0.19.0
Capybara 2.13
Selenium-webdriver 3.6
Run Code Online (Sandbox Code Playgroud)

这是我的硒配置:

Capybara.register_driver :selenium do |app|
  require 'selenium/webdriver'
  Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
Run Code Online (Sandbox Code Playgroud)

一切都在码头上运行.

ruby firefox selenium rspec capybara

0
推荐指数
1
解决办法
1493
查看次数

使用Rspec测试AJAX:此代码有效但规范无法找到路由

我在页面中有这个Javascript:

<script type='text/javascript'>
  function update_price(order_id, quantity) {
    $.ajax({
      beforeSend: function(xhr) {
        xhr.setRequestHeader(
          'X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
      },
      url: "/carts/" + <%= @cart.id %> + "/update_quantity", #you are missing 's' here is this a typo?
      type: "POST",
      data: {
        "order_id" : order_id,
        "quantity" : quantity }
    });
  }
</script>
Run Code Online (Sandbox Code Playgroud)

它适用于用户.匹配的路由如下所示:

resources :carts, only: %i[update destroy index] do
  member do
    match 'update_quantity', to: 'carts#update_quantity', via: %i[get post]
  end
end
Run Code Online (Sandbox Code Playgroud)

我的规格看起来像这样:

it 'updates the quantity' do
  post "/cart/#{cart.id}/update_quantity", xhr: true, params: {
    order_id: cart.orders.first.id,
    quantity: …
Run Code Online (Sandbox Code Playgroud)

javascript ruby ajax rspec ruby-on-rails

0
推荐指数
1
解决办法
634
查看次数

单元测试(Rspec)无法在Rails中更新`current_user`

我有这种类型的单元测试 -

describe "#test_feature" do
  it "should test this feature" do
    sign_in @group_user
    get :get_users, {name: "darpan"}
    expect(@group_user.user_detail.name).to eq("darpan")
    sign_out @group_user
  end
end
Run Code Online (Sandbox Code Playgroud)

和功能get_users是这样的 -

def get_users
  quick_start = current_user.user_detail.quick_start
  current_user.user_detail.update_attribute(:name, "darpan")
  render :json => :ok
end
Run Code Online (Sandbox Code Playgroud)

现在,当我在上面运行rspec, current_useruser_detail被更新(有检查binding.pry),但登录用户@group_useruser_detail未更新.因此我expect失败了.

我在测试这个函数时做错了什么?

ruby unit-testing rspec ruby-on-rails

0
推荐指数
1
解决办法
49
查看次数

Rspec:测试模型范围的响应

我正在为模型范围编写一个非常简单的测试:

describe "scope tests" do

  let(:account1) { create(:account, last_seen: Date.today)}
  let(:account2) { create(:account, last_seen: Date.today - 4.days)}
  let(:account3) { create(:account, last_seen: Date.today - 10.days)}
  let(:d) { Date.today}
  let(:seen_between) { Account.seen_between(d - 14.days, d - 2.days) }

  it "response to seen_between" do
    expect(seen_between).to eq([account3, account2])
    expect(seen_between).to_not include(account1)
  end
end
Run Code Online (Sandbox Code Playgroud)

但是我遇到了一些非常冗长的错误:

Failure/Error: expect(Account.all).to eq([account3, account2])

       expected: [#<Account id: 2, date_of_birth: nil, description: nil, active: true, created_at: "2018-05-10 12:05:4..._lead_id: nil, account_category_id: nil, psychiatrisch: nil, oggz: nil, problem: nil, actions: nil>]
            got: #<ActiveRecord::Relation [#<Account …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

0
推荐指数
1
解决办法
731
查看次数

使用rspec测试cancancan能力

我正在尝试使用rspec测试我的cancancan能力

但与测试特定用户可以做什么相反,我试图测试用户不应该做什么.

现在,我有一个像这样的上下文块:

context "for a manager" do
  before do
    @manager = FactoryGirl.build(:user, :manager)
    @ability = Ability.new(@manager)
  end

  it "should not be able to create Questions" do
    expect(@ability).not_to be_able_to(:create, Question.new)
  end

  it "should not be able to read Questions" do
    expect(@ability).not_to be_able_to(:read, Question.new)
  end

  it "should not be able to update Questions" do
    expect(@ability).not_to be_able_to(:update, Question.new)
  end

  it "should not be able to delete Questions" do
    expect(@ability).not_to be_able_to(:destroy, Question.new)
  end
end
Run Code Online (Sandbox Code Playgroud)

这清楚地表明,类型的用户manager不应该具有对Question模型的任何形式的访问.

是否有一种直接的方法可以在一个块中编写整个it块,只有一个块expect …

rspec ruby-on-rails rspec-rails cancancan

-1
推荐指数
1
解决办法
2199
查看次数

如何查看Rspec/Capybara页面上是否存在链接?

我在水豚有2个场景.第一个检查页面上是否存在链接,第二个检查是否存在链接.

我能够通过使用以下方法获得第一次测试:

expect(page).to have_link('act_id')
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下方法进行第二次测试.

expect(page).to not_have_link('act_id')
Run Code Online (Sandbox Code Playgroud)

但我得到以下错误

NoMethodError: undefined method `not_have_link'
Run Code Online (Sandbox Code Playgroud)

如何测试页面上是否存在链接?

Ruby版本:2.2

Rails版本:4.2

ruby rspec ruby-on-rails capybara

-1
推荐指数
1
解决办法
879
查看次数

Ruby中的参数错误

我有这个代码

   require_relative 'die'

   describe Die do
   describe '#initialize' do
     it 'expects a single argument' do
        expect(Die.instance_method(:initialize).arity).to eq 1
     end

     it 'raises ArgumentError if sides are < 1' do
        expect {              #####line 10
            Die.new(-1)
        }.to raise_error(ArgumentError)

        expect {
            Die.new(0)
        }.to raise_error(ArgumentError)
     end
   end
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

> Die#initialize raises ArgumentError if sides are < 1
   Failure/Error: expect {
     expected ArgumentError but nothing was raised
   # ./spec.rb:10:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

我已标记第10行。有什么帮助我如何消除此错误?

这是我的DIE课程

    class Die
      def initialize(sides)
      end
      def num_of_sides()
      end
     def …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails

-3
推荐指数
1
解决办法
844
查看次数

`method_missing':未定义的局部变量或方法`ture'for#<Class:0x007f9922f68110>(NameError)

大家好我正在学习如何使用RSpec测试代码,当我尝试运行Rspec时,我遇到了这个错误.我认为是Active Record上的一个错误,但我搜索谷歌搜索,我没有什么可以帮助我...所以我的错误是这样的:

$ rspec
/Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing': undefined local variable or method `ture' for #<Class:0x007f9922f68110> (NameError)
    from /Users/romenigld/workspace/ebooks/everyday_rails_Testing_with_RSpec/contact_app/app/models/contact.rb:6:in `<class:Contact>'
    from /Users/romenigld/workspace/ebooks/everyday_rails_Testing_with_RSpec/contact_app/app/models/contact.rb:1:in `<top (required)>'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:360:in `require_or_load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/romenigld/workspace/ebooks/everyday_rails_Testing_with_RSpec/contact_app/spec/models/contact_spec.rb:3:in `<top (required)>'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/romenigld/.rvm/gems/ruby-2.3.3/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run' …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails

-3
推荐指数
1
解决办法
460
查看次数