我从rspec的2.99升级到3.0.3 RSPEC和具有转换实例方法使用allow_any_instance_of,但还没有想出如何存根类方法.我有这样的代码:
module MyMod
class Utils
def self.find_x(myarg)
# Stuff
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的rspec 2测试做到了这一点:
MyMod::Utils.stub(:find_x).and_return({something: 'testing'})
Run Code Online (Sandbox Code Playgroud)
什么是Rspec 3的做法?
我是第二次做Rails教程.当我进入这个
rails generate integration_test static_pages
Run Code Online (Sandbox Code Playgroud)
我得到的spec/rails_helper.rb和spec/spec_helper.rb,而不是仅仅spec/spec_helper.rb
现在,当我运行我的测试时,它们比我上次这样做时更长(更"冗长")并且更慢.我想知道两个文件之间有什么区别,如果我做错了什么.另外,有没有办法摆脱rails_helper.rb文件而不搞乱一切?
我将我升级rspec-rails到3.0.1,现在我在所有测试中都看到了这个错误
Failure/Error: Sidekiq::Status::complete?(json.jid).should be_true
expected true to respond to `true?`
Run Code Online (Sandbox Code Playgroud)
我找不到解决方案,也找不到我所缺少的.
在RSpec中,特别是版本> = 3,之间有什么区别:
allow设置了返回测试双打参数,消息的预期,然后使用expect,使在返回的测试双打断言expect用于设置参数的期望并返回测试双精度或者只是语义学?我知道提供/指定返回值expect是RSpec模拟2.13中的语法,但据我所知,RSpec模拟3中的语法更改使用allow.
但是,在下面的(传递)示例代码中,使用allow/ expect或只是expect/ and_return似乎生成相同的结果.如果一种语法比另一种语言更受青睐,也许我会期望有某种弃用通知,但由于没有,似乎两种语法都被认为是有效的:
class Foo
def self.bar(baz)
# not important what happens to baz parameter
# only important that it is passed in
new
end
def qux
# perform some action
end
end
class SomethingThatCallsFoo
def some_long_process(baz)
# do some processing
Foo.bar(baz).qux
# do other processing
end
end
describe SomethingThatCallsFoo do
let(:foo_caller) { SomethingThatCallsFoo.new …Run Code Online (Sandbox Code Playgroud) 我正在运行这部分测试:
describe Dictionary do
before do
@d = Dictionary.new
end
it 'can check whether a given keyword exists' do
@d.include?('fish').should be_false
end
Run Code Online (Sandbox Code Playgroud)
使用此代码:
class Dictionary
def initialize
@hash = {}
end
def add(new_entry)
new_entry.class == String ? @hash[new_entry] = nil : new_entry.each { |noun, definition| @hash[noun] = definition}
end
def entries
@hash
end
def keywords
@hash.keys
end
def include?(word)
if @hash.has_key?(word)
true
else
false
end
end
end
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么,但我的测试仍然失败并说:
> 1) Dictionary can check whether a given keyword exists
> Failure/Error: …Run Code Online (Sandbox Code Playgroud) 我在大多数测试中都有以下代码:
describe 'index'
let(:company) { FactoryGirl.create(:company) }
let(:user) { FactoryGirl.create(:user, company: company) }
before do
sign_in user
visit products_path
end
...
end
Run Code Online (Sandbox Code Playgroud)
但我收到以下警告:
WARNING: let declaration 'user' accessed in a 'before(:all)'
Run Code Online (Sandbox Code Playgroud)
我的问题是,这样做的正确方法是什么?我找不到有关警告本身的更多信息.
谢谢!
编辑:我的目标是使用用户变量,以便我可以将其传递给sign_in,签名用户,然后在另一个测试中使用它(我检查用户的公司属性)
我正在尝试遵循code.tuts的指南,我一直收到错误.
这是我的图书馆规范:
require 'spec_helper'
describe Library do
before :all do
lib_arr = [
Book.new("JavaScript: The Good Parts", "Douglas Crockford", :development),
Book.new("Dont Make me Think", "Steve Krug", :usability),
]
File.open "books.yml", "w" do |f|
f.write YAML::dump lib_arr
end
end
before :each do
@lib = Library.new "books.yml"
end
describe "#new" do
context "with no parameters" do
it "has no book" do
lib = Library.new
expect(lib).to have(0).books
end
end
context "with a yaml file name parameters" do
it "has two books" …Run Code Online (Sandbox Code Playgroud) 我将rspec版本从2升级到3.这是我遇到的问题之一:
Failures:
1) Slide after .destroy(force: false) visible if .with_deleted
Failure/Error: expect{@slide.destroy(force: false)}.to_not change(Slide.with_deleted, :count).by(1)
NotImplementedError:
`expect { }.not_to change { }.by()` is not supported
# ./spec/models/slide_spec.rb:36:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
在rspec的更改日志中,我可以读到它从未被支持过(oink?!@#).同时,仍然有一些示例如何使用更改语法但没有not关键字.
所以问题是如何期待没有变化?
我正在尝试为需要身份验证的某些RSpec请求设置标头.标题是ACCESS_TOKEN.无论我如何设置标头,它都永远不会被设置.我知道应用程序有效,因为我可以手动测试它,我只是不能让rspec测试工作.请在此处查看此问题的完整源代码和测试:https://github.com/lightswitch05/rspec-set-header-example
由于在我的大多数请求规范中都使用了身份验证,因此我创建了支持帮助程序模块来检索访问令牌并将其设置在标头中.下面是我如何设置标题的摘要,查看我在完整源代码中尝试过的所有内容
# my_app/spec/support/session_helper.rb
module SessionHelper
def retrieve_access_token
post api_v1_session_path({email: 'test@example.com', password: 'poor_password'})
expect(response.response_code).to eq 201
expect(response.body).to match(/"access_token":".{20}"/)
parsed = JSON(response.body)
token = parsed['access_token']['access_token']
@request.headers['HTTP_ACCESS_TOKEN'] = token
end
end
Run Code Online (Sandbox Code Playgroud)
一个使用此帮助程序的示例请求规范应该可以工作,但总是失败,因为标头永远不会被设置:
# my_app/spec/requests/posts_spec.rb
# ...
context "create" do
it "creates a post" do
retrieve_access_token
post = FactoryGirl.build(:post)
post api_v1_posts_path(
post: {
title: post.title,
content: post.content
}
)
expect(response.body).to include('"id":')
expect(response.body).to include('"title":"' + post.title + '"')
expect(response.body).to include('"content":"' + post.content + '"')
expect(response.response_code).to eq …Run Code Online (Sandbox Code Playgroud) 我正在从rspec 2升级到rspec 3,并希望使用新语法而不启用旧语法.但我有一些我在顶级before(:each)块中设置的存根,我有选择地unstub想要原始实现.
当我使用新allow语法定义存根时,是否有一些等效方法来删除存根?