标签: rspec-expectations

如何在irb中使用RSpec期望值

我想[1,2,3].should include(1)在irb中使用.我试过了:

~$ irb
1.9.3p362 :001 > require 'rspec/expectations'
 => true 
1.9.3p362 :002 > include RSpec::Matchers
 => Object 
1.9.3p362 :003 > [1,2,3].should include(1)
TypeError: wrong argument type Fixnum (expected Module)
    from (irb):3:in `include'
    from (irb):3
    from /home/andrey/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `<main>'
Run Code Online (Sandbox Code Playgroud)

但它不起作用虽然它是一个有效的案例.我该怎么用[1,2,3].should include(1)

ruby rspec irb rspec-expectations

11
推荐指数
1
解决办法
3114
查看次数

如何在Rspec之外使用rspec期望和匹配器?

我有一个脚本,它已经发展成需要做一些断言和匹配.

它是用ruby编写的,我已经包含rspec在Gemfile中并且需要它.

我发现这个非常有用的SO帖子如何使用irb:

如何在irb中使用RSpec期望值

我还发现了以下内容:

在描述...阻止之外使用RSpec的"期望"等

class BF
   include ::Rspec::Matchers

   def self.test
     expect(1).to eq(1)
   end
end

BF.test
Run Code Online (Sandbox Code Playgroud)

我在线上得到一个错误expect.

ruby rspec rspec-expectations

6
推荐指数
1
解决办法
442
查看次数

我可以在自定义匹配器中使用内置的RSpec匹配器吗?

我对功能规格有以下期望(相当低级,但仍然有必要):

expect(Addressable::URI.parse(current_url).query_values).to include(
  'some => 'value',
  'some_other' => String
)
Run Code Online (Sandbox Code Playgroud)

请注意,第二个查询值是模糊匹配,因为我只想确保它在那里,但是我不能更具体地说明它。

我想将其提取到自定义匹配器中。我开始于:

RSpec::Matchers.define :have_query_params do |expected_params|
  match do |url|
    Addressable::URI.parse(url).query_values == expected_params
  end
end
Run Code Online (Sandbox Code Playgroud)

但这意味着我无法通过{'some_other' => String}那里。为了继续使用模糊匹配,我必须include在自定义匹配器中使用匹配器。

但是,其中的所有内容RSpec::Matchers::BuiltIn都标记为专用API,Include具体记录为:

# Provides the implementation for `include`.
# Not intended to be instantiated directly.
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是:RSpec支持在自定义匹配器中使用内置匹配器吗?我该怎么做?

ruby rspec rspec-expectations

5
推荐指数
1
解决办法
1142
查看次数

黄瓜未定义的方法

我使用的是cucumbercapybara

我有几个类似的错误.

对于步骤:

Then /I should see movies of rating 'PG' or 'R'/ do
  page.body.should match(/<td>PG<\/td>/)
  page.body.should match(/<td>R<\/td>/)
end
Run Code Online (Sandbox Code Playgroud)

黄瓜错误:

undefined method `match' for #<Cucumber::Rails::World:...> (NoMethodError)
./features/step_definitions/movie_steps.rb:37:in 
   `/I should see movies of rating 'PG' or 'R'/'
Run Code Online (Sandbox Code Playgroud)

对于步骤:

Then /I should see an empty table/ do
  page.body.scan(/<tr>/).length.should == 0
end
Run Code Online (Sandbox Code Playgroud)

黄瓜错误:

undefined method `should' for 1:Fixnum (NoMethodError)
./features/step_definitions/movie_steps.rb:46:in 
      `/I should see an empty table/'
Run Code Online (Sandbox Code Playgroud)

并为步骤:

Then /I should see all of the movies/ do
  Movie.find(:all).length.should page.body.scan(/<tr>/).length
end

undefined …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails cucumber rspec-expectations

4
推荐指数
1
解决办法
5667
查看次数

rspec:未定义的局部变量或方法`be_true'

我使用的是rspec 2.4.0和黄瓜0.6.4.我正在运行一个简单的场景(为了这个问题):

Scenario: Simple Test
When I test something
Run Code Online (Sandbox Code Playgroud)

步骤定义:

require 'rspec'
require 'rspec/expectations'

When /^I test something$/ do
  result = (1==1)
  result.should be_true
end
Run Code Online (Sandbox Code Playgroud)

当我运行这个场景时,我遇到以下问题:

 undefined local variable or method `be_true' for #<Object:0x1b3b424> (NameError)
Run Code Online (Sandbox Code Playgroud)

我也使用bundler来管理我的依赖项.

我在做一些明显不对的事吗?

问候,

标记

rspec cucumber bundler rspec2 rspec-expectations

2
推荐指数
1
解决办法
1679
查看次数

我如何在页面对象类中使用Rspec Expectations

如何在页面对象类中使用Rspec期望.我需要断言元素.目前我正在使用xpath,其他定位器来检查元素是否存在.我知道使用它是步骤定义.但我需要在课堂上.

码:

class AssertTest
include PageObject

span(:success, text: "Message added successfully")

def assert_element
    success_element.when_present (or) success?
    # I need to use Rspec expectations instead

    # continue...
end
end
Run Code Online (Sandbox Code Playgroud)

在步骤定义中,我可以像以下一样使用它:

@current_page.text.should include "message"
Run Code Online (Sandbox Code Playgroud)

ruby rspec cucumber page-object-gem rspec-expectations

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

如何执行一次并期望使用 RSpec 进行多次更改?

如果我想测试一个操作会产生某些副作用,我如何执行一次操作并使用changerspec 匹配器。例子:

expect { some_method }.to change(Foo, :count).by(1)
expect { some_method }.to change(Bar, :count).by(1)
expect { some_method }.to change(Baz, :count).by(1)
Run Code Online (Sandbox Code Playgroud)

我该如何执行 some_method只一次,而不是 3 次?

或者我需要做类似的事情:

foo_count_before = Foo.count
bar_count_before = Bar.count
baz_count_before = Baz.count

some_method

foo_count_after= Foo.count
bar_count_after= Bar.count
baz_count_after= Baz.count

expect(foo_count_after - foo_count_before).to eq 1
expect(bar_count_after - bar_count_before).to eq 1
expect(baz_count_after - baz_count_before).to eq 1
Run Code Online (Sandbox Code Playgroud)

ruby testing rspec rspec-expectations

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

黄瓜水豚换行符

我正在将黄瓜和水豚一起使用。我注意到,在安装捆绑软件之后,突然之间,我的测试失败了,新的换行符作为文本的一部分出现在字符串中。

错误示例:

RSpec::Expectations::ExpectationNotMetError: expected to find text "Longview Road Clase Swansea SA6 7JL" in "Skip to main content\nGOV.UK\nDigital tachograph card\n......."
Run Code Online (Sandbox Code Playgroud)

过去,那些换行符不存在。我正在努力寻找导致此问题的宝石。

有没有一种方法可以停止此操作,而无需在我从网页提取的每个字符串中都做一个剥离?

一些gem版本:

水豚-2.18 Rspec期望-3.7.0黄瓜-2.4.0

cucumber capybara rspec-expectations

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