我正在学习Rails这个古老的方式.通过阅读Agile Web Development with Rails(第3版)作为起点.我目前正在教授测试的章节.我也知道其他BDD测试框架,如RSPec.所以我想知道RSpec,Cucumber,Shoulda等框架是否取代了知道/使用Test :: Unit的需要?它们本身是否相关并用于不同目的?
更新:由于我是测试的新手,我也希望获得有关哪些资源对于使用不同框架的方式有用的反馈.
谢谢!
试图找到但没有成功.只是想知道如何在Rails 3中测试范围.
可以使用rspec,shoulda或只是一个测试单元.
谢谢.
实际上,我尝试这种方式,但它不是完整的测试,因为它仍然需要放置order()方法.
范围:
scope :recents_available, where(:available => true, :locked => false).order("created_at DESC")
describe Job, ":recents_available" do
it "should have the scope" do
Job.should respond_to(:recents_available)
end
it "should include recents jobs that are available and unlocked" do
@job = Factory(:job, :available => true, :locked => false )
Job.recents_available.should include(@job)
end
Run Code Online (Sandbox Code Playgroud)
结束
我正在尝试在Ruby on Rails中使用Test :: Unit和Shoulda设置Factory Girl.我已经安装了gem,在test/factories目录下创建了我的工厂文件,并在test/models目录下创建了我的spec文件.我得到的当前错误是'ArgumentError:No such factory:test',这让我相信test_factory.rb文件没有被加载?知道我应该改变什么?
这是我的文件.
#test/factories/test_factory.rb
Factory.define :test do |t|
t.name 'test_spotlight'
t.label 'test spotlight label'
end
Run Code Online (Sandbox Code Playgroud)
和
#test/modes/test_spec.rb
require 'test_helper'
require 'factory_girl'
class TestTest < Test::Unit::TestCase
def setup
@test = Factory.build(:test)
end
context "A test" do
should "save with the minimum requirements" do
assert @test.save
end
end
end
Run Code Online (Sandbox Code Playgroud) 当我运行rspec spec/models结果是好的.
但是当我使用spork时,每个测试应该在哪里进行宏(如it { should validate_presence_of(:title) }使用FAILS,如下所示:undefined method 'validate_presence_of' for ...
我用:
rails (3.0.0)
shoulda (2.11.3)
spork (0.8.4)
rspec-rails (>= 2.0.0.beta.22)
Run Code Online (Sandbox Code Playgroud)
投机/ spec_helper.rb:
require 'rubygems'
require 'spork'
Spork.prefork do
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda'
...
我正在使用带有rails的shoulda-matcher,我正在创建一个名为"comments"的模型和另一个名为"post"的模型.评论是多态的.
当我在帖子中使用shoulda匹配器进行测试时,就像这样
it {should have_many(:comments)}
Run Code Online (Sandbox Code Playgroud)
它收到了这条消息
预期Post有一个名为comments的has_many关联(Comment没有post_id外键.)
在我的评论模型中,我有
belongs_to :commentable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
如何测试我的多态关联,以便帖子可以有很多注释?
ps the shoulda matcher文档说它支持多态关联.
我的avatar_parts_spec.rb中有一个shoulda匹配器,我无法通过它:
测试:
require 'rails_helper'
RSpec.describe AvatarPart, :type => :model do
it { should validate_presence_of(:name) }
it { should validate_presence_of(:type) }
it { should validate_uniqueness_of(:name).case_insensitive }
it { should belong_to(:avatar) }
end
Run Code Online (Sandbox Code Playgroud)
模型:
class AvatarPart < ActiveRecord::Base
attr_accessible :name, :type, :avatar_id
belongs_to :avatar
validates_uniqueness_of :name, case_sensitive: false
validates :name, :type, presence: true, allow_blank: false
end
Run Code Online (Sandbox Code Playgroud)
移民:
class CreateAvatarParts < ActiveRecord::Migration
def change
create_table :avatar_parts do |t|
t.string :name, null: false
t.string :type, null: false
t.integer :avatar_id
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
错误: …
我的应用程序中的某些功能根据客户端的IP地址而有所不同.有没有办法在Rails功能测试中测试?我正在使用Test :: Unit和Shoulda.
我有以下Rspec测试:
describe Productlimit do
before(:each) do
@productlimit = Factory.create(:productlimit, :user => Factory.create(:user))
end
subject { @productlimit }
...
it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
...
end
Run Code Online (Sandbox Code Playgroud)
但我得到以下令人困惑的错误:
1) Productlimit
Failure/Error: it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
Expected errors to include "has already been taken" when price_cents is set to 9530, got errors: ["direction_down has already been taken (false)"]
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?我不明白为什么这不起作用,因为错误信息似乎是正确的?
编辑:
在其他情况下也会发生这种情况:
# product_spec.rb
...
it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") }
# …Run Code Online (Sandbox Code Playgroud) 我在使用shoulda测试设计时遇到了一些困难:
2) Error:
test: handle :index logged as admin should redirect to Daily page.
(Admin::DailyClosesControllerTest):
NoMethodError: undefined method `env' for nil:NilClass
devise (1.0.6) [v] lib/devise/test_helpers.rb:52:in
`setup_controller_for_warden'
Run Code Online (Sandbox Code Playgroud)
我在test_helper中有这个:
include Devise::TestHelpers
Run Code Online (Sandbox Code Playgroud)
想法?提前致谢,
克里斯提
我是单元测试场的新手; 我现在只使用了大约2个月的单元测试.当我在Ruby中进行单元测试时,我目前遵循TDD风格并使用Test :: Unit :: TestCase.我还阅读了有关RSpec以及它如何遵循BDD方法的内容.我也读过关于Shoulda的内容,它介于两个框架之间.我的问题是,我应该把时间花在RSpec或Shoulda上,或者我应该坚持使用Test :: Unit :: TestCase?任何有关该主题的意见表示赞赏.
shoulda ×10
rspec ×4
ruby ×4
unit-testing ×3
testunit ×2
bdd ×1
cucumber ×1
devise ×1
factory-bot ×1
polymorphism ×1
rspec2 ×1
spork ×1
sqlite ×1
testing ×1
validation ×1