我想[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在Gemfile中并且需要它.
我发现这个非常有用的SO帖子如何使用irb:
我还发现了以下内容:
class BF
include ::Rspec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test
Run Code Online (Sandbox Code Playgroud)
我在线上得到一个错误expect.
我对功能规格有以下期望(相当低级,但仍然有必要):
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支持在自定义匹配器中使用内置匹配器吗?我该怎么做?
我使用的是cucumber与capybara
我有几个类似的错误.
对于步骤:
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 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期望.我需要断言元素.目前我正在使用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) 如果我想测试一个操作会产生某些副作用,我如何执行一次操作并使用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) 我正在将黄瓜和水豚一起使用。我注意到,在安装捆绑软件之后,突然之间,我的测试失败了,新的换行符作为文本的一部分出现在字符串中。
错误示例:
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