遵循RailsTutorial,我一直收到这个错误消息(实际上是四个错误消息之一),而它应该是工作的:
UsersController authentication of edit/update pages for signed-in
users should require matching users for 'edit'
Failure/Error: @users = [@user, second, third] undefined local variable
or method 'second' for
#<RSpec::Core::Examp
这是我编写的规范代码。我试图为一个数组(Payments)做一个not,但是当我试图在it块的顶部添加let变量时,它告诉我这个变量或方法不存在。所以我假设变量没有被调用。
有谁能看看我应该如何重新规范化我的规范,这样我就可以这样做了:
payments.each do |p|
it "updates the payment record of #{p.id} with a status of #{p.status}" do
payments.each do |p|
payment = Payment.find_by_id(p.id)
pay
我正在尝试从Helpers模块中递归地声明Rspec中的上下文。
(我的代码将以一种不寻常的方式使用这些上下文,即递归地对嵌套哈希中的键进行断言。也许我可以用另一种方式解决这个问题,但这不是重点。)
一个最起码的完整例子是:
module Helpers
def context_bar
context "bar" do
it "baz" do
expect(true).to be true
end
end
end
end
include Helpers
describe "foo"
我试图在请求规范中设置一个会话变量。
我尝试了以下几种方法:
RSpec.describe 'Application Controller' do
context 'updating an application' do
before :each do
@app = create(:application, step: 'investigation')
end
it 'should update app status' do
Status.create(app_id: @app
例如,在Rails的gemfile中:
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
do ... end语句是怎么回事?使用rspec:
describe "Foo" do
context "bar" do
expect...
end
end
do ... end-s是否正在创建一个块,其中的信息正在其他地方使用?如果不设置块参数,这是如何工作的?
我正在尝试访问我在RSpec中定义的RSpec模块中的实例变量,但我似乎无法做到这一点。
一个经过简化的规范文件显示了我遇到的问题:
my_spec.rb
require 'rspec'
describe 'passing instance variables from specs into ruby mixins' do
it 'should pass the instance variable to the module' do
@a = 'a'
A.a.should == 'a'
e
在rspec测试套件中,测试模块是用来评估包含模块的类的行为的测试。下面是一个简单的例子。
describe "MyMod" do
module MyMod; end
shared_examples_for "a class" do |klass|
it "includes the module" do
klass.included_modules.include? MyMod.should be_true
end
end
describe "when included in a cl
在Rspec中,我想利用super()来调用已定义的let块(如果它存在的话),或者设置一个新的值(如果没有),我想在shared_example组中使用它,但是我无法找到如何做到这一点。
我试过检查@some_let是否存在,我尝试过检查:some_let的超级方法是否属于kernel,而且它们都没有提供任何有用的东西;我无法访问instance_methods或instance_method,因为Rspec不允许我,并且在互联网上搜索一个方法还没有找到答案。
我想做这样的事:
shared_examples 'a shared example' do
let(:some
我使用的是Rspec 2.0。我不明白这个作用域是如何工作的……不知何故,你可以读取任何块中的变量,但我不能更新它?为什么会这样呢?
describe 'Test App' do
before(:all) do
@test= :blah
end
it 'test' do
@test=:bye
p @test # => prints bye
end
it 'test' do
p @test # => prints blah rather than bye...
e
我目前正在研究Rails 4测试处方,并且遇到了我的规范文件的加载顺序问题,这本书中没有提到这个问题。不幸的是,我无法从我在这个问题上找到的东西中解决问题。在尝试运行测试时,我得到的输出是
/Users/Admin/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/example_group.rb:370:in `find_and_eval_shared': Could not find shared examples "sizeable" (ArgumentError)
from /Users/
我有一个类似于以下的课程:
class Foo
@variable = {}
class << self
attr_accessor :variable
end
def self.run(file)
# do stuff
end
end
我有一个RSpec类型的方法,如下所示:
def method?
Foo.variable[@name] = {}
Foo.variable[@name][:variable_two] = 'string'
true
end
我有一个rspec和自定义任务,如下所示:
RSpec
我刚开始在Rails应用程序中使用RSpec,我无法让RSpec看到我的模型:
我在我的Gemfile中添加了以下行
group :development, :test do
gem 'rspec-rails'
end
并安装在
rails generate rspec::install
然后我在app/models/目录中创建了一个app/models/文件,它是而不是 ActiveModel
class FacebookPage
class << self
# define class variables
attr_accessor :a
我目前正在使用rspec和cancan。我意识到在我的控制器规范文件中到处都是权限控制测试用例,这是非常混乱的。基本上,我的几乎每个控制器规范文件都有类似的内容:
describe "failure" do
it { get :new }
it { get :edit, :id => @deal }
it { get :update, :id => @deal }
it { get :destroy, :id => @deal }
after(:each) do
response.should_not
我有一个像这样的规格
describe MyClass do
it_behaves_like SharedClass, MyClass.new
end
在我的共享示例规范中,我有
shared_examples_for SharedClass do |instance|
before do
instance.some_my_class_method = double
end
# some specs here
end
在MyClass实例中,有一些方法不能存根到shared_examples_for块中,所以我希望在将它们传递到it_behaves_like语句之前先
我尝试了以下几点:
describe "#check_recurring_and_send_message" do
let(:schedule) {ScheduleKaya.new('test-client-id')}
context "when it is 11AM and recurring event time is 10AM" do
schedule.create_recurring_event('test-keyword', 'slack', 'day'
这是我的主要密码。我在用rspec
require_relative "rpn_calculator"
describe RPNCalculator do
let(:calculator) {RPNCalculator.new}
it "adds two numbers" do
calculator.push(1)
calculator.push(3)
calculator.plus
expect(calculator.value).to eq 4
end
这是我在rpn_calculator中的代码
class RPNCalc
我正在与下面的RSpec做斗争。为什么要这样做:
it 'GET articles#new creates new instance of Article' do
get :new
expect(assigns[:article]).to be_a(Article)
end
这两种情况都没有(发现了一些不同括号的例子,这就是我决定检查这两种可能性的原因)。
subject { get :new }
it { expect(assigns[:article]).to be_a(Article) }
it { expect(assigns(:a
我从视图规范中得到了一些奇怪的结果
Failure/Error: assert_select "div.fileinput-new.thumbnail"
ArgumentError:
wrong number of arguments (3 for 1)
好的,这似乎是不言自明的(虽然我不确定引用的3个参数是什么)。
然而,如果我再次运行套件,我可能会得到0错误。接下来是5个错误,与上面的类型相同,但引用了不同的DOM元素。
这些规格中没有什么特别之处。
describe do
build :object / create :object
@object = a
在rspec 1中我可以做到
describe "Something", :shared => true do
include SomeModule # which has the :a_method method
def a_method(options)
super(options.merge(:option => @attr)
end
it "foofoofoo" do
end
end
describe "Something else" do
before(:each) do
@attr = :
我刚更新了我的个人档案。
起初,我认为问题来自Zeitwerk (从2.4.2到2.5.4),但我降低了它的等级,我的规范仍然有问题。我孤立地指出,问题并不来自于RSpec和依赖关系。
实际上,RSpec没有找到在另一个文件中定义并且与文件名/类名不匹配的类。
要点:发现了 Filter::MyStandardError。
# app/services/filter/my_standard_error.rb
module Filter
class MyStandardError < StandardError; end
class MySpecificError < My
我试图将RSpec中的代码分割成多个文件,这样看起来就更好了。当前文件如下所示。
require 'rails_helper'
RSpec.describe Api::MyController do
let(:var1) {}
let(:var2) {}
it 'should calculate some value' do
expect(var1 + var2).to eq('some value')
end
end
现在,它是如何处理重构的。
require 'rails_helper'
我刚接触过RSpec,做了一些研究,发现有几种方法可以从网上帖子中进行数据驱动的枚举测试,但是它们并没有像完整的教程那样涉及到很多细节。所以,在我再次详细回顾那些在线文章之前,我想先问一下。
下面的设置是基于使用RSpec的标准简单方式(定义了描述&它块,而不是导入RSpec的部分来实现期望)。然后,我尝试添加数据驱动的功能:
require 'rspec'
require 'csv'
describe "test suite name" do
before :all do
#this CSV mapping method
我是RSpec的新手,我想创建一个运行规范的自定义顺序,我希望最重要的规范首先运行,然后运行不太重要的规范,以此类推,直到最不重要的规范。Rspec已经有了默认的全局排序:defined,但我想覆盖它并创建我自己的自定义排序。我读过一篇关于这方面的文档(“ "),但不明白如何实现它。如果你们能发布一些带有解释的示例代码,将不胜感激。
这是我的spec_helper.rb文件:
RSpec.configure do |config|
#config.register_ordering :global do |examples|
# model, other = examples.
当涉及到我的HTML标记时,我非常严格,我对表单、列表等遵循严格的编码约定……
我希望在我的RSpec测试中包含可重用的测试,这样我就可以从任何其他测试中调用表单测试,并将其直接指向我正在测试的页面或网址。
如下所示:
# spec/helpers/form_tester.rb
describe FormTester
it "should check to see if all the text fields have an ID prefix of 'input-'" do
... @form should be valid ...
sho