使用RSpec测试我的控制器时遇到问题 - response.body调用总是返回一个空字符串.在浏览器中,一切都正确呈现,黄瓜特征测试似乎正确,但RSpec每次都失败.
对响应对象的其他期望,例如response.should render_template('index')传递没有任何问题.
有没有人遇到过这个问题?也许响应html可以通过其他方式获得?
至于版本,Rails 2.1.0,RSpec 1.2.7.
鉴于我Personable在我的Rails 4应用程序中有一个问题有一个full_name方法,我将如何使用RSpec进行测试?
关注/ personable.rb
module Personable
extend ActiveSupport::Concern
def full_name
"#{first_name} #{last_name}"
end
end
Run Code Online (Sandbox Code Playgroud) 现在,如果我使用我的测试套件rake spec我得到一个错误:
1) SegmentsController GET 'index' should work
Failure/Error: get 'index'
undefined method `locale' for #
# ./spec/controllers/segments_controller_spec.rb:14:
in `block (3 levels) in '
这是正常的,因为我有一个错误:)
问题是跟踪不是很有帮助.我知道它突破了segments_controller_spec.rb,第14行,但这只是我称之为测试的地方:
### segments_controller_spec.rb:14
get 'index'
Run Code Online (Sandbox Code Playgroud)
我更喜欢实际的断行和完整的跟踪,而不是spec文件夹中的部分.
跑步--trace并没有帮助.
如何解决Capybara的歧义?出于某种原因,我需要在页面中使用相同值的链接,但由于我收到错误,因此无法创建测试
Failure/Error: click_link("#tag1")
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link "#tag1"
Run Code Online (Sandbox Code Playgroud)
我无法避免这种情况的原因是因为设计.我正在尝试使用右侧的推文/标签和页面左侧的标签重新创建推特页面.因此,相同的链接页面不可避免地出现在同一页面上.
我有一个Rails应用程序,在我的RSpec测试中有超过2,000个例子.毋庸置疑,这是一个很大的应用程序,还有很多需要测试的地方.在这一点上运行这些测试是非常低效的,并且因为它需要很长时间,所以在推动新构建之前我们几乎不鼓励编写它们.我在我的spec.opts中添加了--profile来查找运行时间最长的示例,其中至少有10个运行平均需要10秒.在RSpec专家中你是正常的吗?一个例子10秒完全太长了吗?我意识到,有2000个例子,需要花费大量时间来彻底测试所有内容 - 但此时4小时有点荒谬.
你看到你运行时间最长的例子有多少次?我可以做些什么来解决我现有的规格,以找出瓶颈并帮助加快速度.在这一点上,每分钟都会有所帮助.
我不能让capybara与rspec合作.它给了我这个错误:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>
Run Code Online (Sandbox Code Playgroud)
我知道有很多关于此的帖子,但没有解决方案对我有用.他们中的大多数涉及的规格不在/ spec/features中 - 我的是.
首先是错误:
$bundle exec rspec spec
F
Failures:
1) security signs users in
Failure/Error: visit "/sessions/new"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>
# ./spec/features/security_spec.rb:4:in `(root)'
Finished in 0.006 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/security_spec.rb:3 # security signs users in
Run Code Online (Sandbox Code Playgroud)
我认为重要的是要注意,起初我使用URL Helper'new_sessions_path'并且它一直给我一个错误undefined local variable or method 'new_sessions_path'.我知道这是有效的,因为:
$ rake routes
logout_sessions GET /sessions/logout(.:format) sessions#logout
sessions POST /sessions(.:format) sessions#create
new_sessions GET /sessions/new(.:format) sessions#new
contracts POST /contracts(.:format) …Run Code Online (Sandbox Code Playgroud) 我从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的做法?
RSpec let与before块之间的区别是什么?
什么时候使用?
下面的例子中有什么好方法(让或之前)?
let(:user) { User.make !}
let(:account) {user.account.make!}
before(:each) do
@user = User.make!
@account = @user.account.make!
end
Run Code Online (Sandbox Code Playgroud)
我研究过这个stackoverflow帖子
但是为上面的关联内容定义let是否合适?
我正在使用RSpec2和Capybara进行验收测试.
我想断言链接在Capybara中是否被禁用.我怎样才能做到这一点?
有人能告诉我,如果我只是以错误的方式进行设置吗?
我有以下具有has_many.through关联的模型:
class Listing < ActiveRecord::Base
attr_accessible ...
has_many :listing_features
has_many :features, :through => :listing_features
validates_presence_of ...
...
end
class Feature < ActiveRecord::Base
attr_accessible ...
validates_presence_of ...
validates_uniqueness_of ...
has_many :listing_features
has_many :listings, :through => :listing_features
end
class ListingFeature < ActiveRecord::Base
attr_accessible :feature_id, :listing_id
belongs_to :feature
belongs_to :listing
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Rails 3.1.rc4,FactoryGirl 2.0.2,factory_girl_rails 1.1.0和rspec.这是我对:listing工厂的基本rspec rspec健全性检查:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
Run Code Online (Sandbox Code Playgroud)
这是工厂(:上市)
FactoryGirl.define do
factory :listing do
headline 'headline'
home_desc 'this is the home …Run Code Online (Sandbox Code Playgroud) rspec ×10
ruby ×5
capybara ×3
unit-testing ×3
testing ×2
bdd ×1
factory-bot ×1
rspec2 ×1
rspec3 ×1