我正在使用带有shoulda的Test :: Unit来测试控制器.
因为我只是测试控制器,所以我不希望渲染视图.
我正在对某些对象进行存根,在渲染视图时会抛出一些错误,但测试不会失败,因为控制器是正确的.
那么,从我的测试中,是否有任何方法可以禁用模板/视图的渲染?
我听说rSpec就是这样的.
我正在使用RSpec + Shoulda在Rails 3中测试我的RESTful控制器.我无法弄清楚如何测试创建动作的重定向.标准RESTful控制器应重定向到新帖子的show动作.例如,如果我有一个Project Model的ProjectsController,那么在成功创建后,该操作应该:
redirect_to project_url(@project)
Run Code Online (Sandbox Code Playgroud)
Shoulda提供了一个方便的redirects_to宏来处理这个问题.这是我尝试过的:
describe ProjectsController, '#create' do
context "Anonymous user" do
before :each do
@attrs = Factory.attributes_for(:project_with_image)
post :create, :project => @attrs
end
it { should assign_to(:project) }
it { should respond_with(:redirect) }
it { should redirect_to(@project) }
end
end
Run Code Online (Sandbox Code Playgroud)
(是的,我正在使用FactoryGirl,但因为在这种情况下我只使用它作为属性,所以它应该没关系.我想.)
如何在那里指定最后一次测试?它应该redirect_to(...)什么?我试过@project,project_url(@project)..但我无法理解.
看看Shoulda匹配器代码,我注意到redirect_to匹配器可以接受一个块.但我不确定如何访问该块中新创建的@project对象......
有什么想法吗?
请帮忙:
我想用minitest用户.
这是我得到的例外:
NoMethodError: undefined method `run_teardown_hooks' for #<#<Class:0x007fd42ed95490>:0x007fd42ed00c78>
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/mocha-0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:39:in `run'
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/activesupport-3.2.11/lib/active_support/testing/setup_and_teardown.rb:36:in `block in run'
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:425:in `_run__1339649173504478165__setup__555511959301366683__callbacks'
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/boti/.rvm/gems/ruby-1.9.2-p180@search_server/gems/activesupport-3.2.11/lib/active_support/testing/setup_and_teardown.rb:35:in `run'
Run Code Online (Sandbox Code Playgroud)
这是我的gemfile:
gem 'minitest'
gem 'minitest-should_syntax'
gem 'miniskirt'
gem 'rack-livereload'
gem 'guard'
gem 'guard-minitest'
gem 'guard-bundler'
gem 'guard-livereload'
gem 'annotate', ">=2.5.0"
gem 'guard-annotate'
gem "rb-fsevent"
gem 'database_cleaner'
gem "minitest-rails-shoulda"
gem 'sqlite3', :require => 'sqlite3'
Run Code Online (Sandbox Code Playgroud)
这是我的test_helper:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/autorun'
require 'minitest/should_syntax'
require 'factories/factories'
class ActionController::TestCase
include Devise::TestHelpers
end
Run Code Online (Sandbox Code Playgroud) 我有一个带验证的模型,如下所示:
class Order
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
validates_presence_of :first_name, :message => "Can't be empty"
validates_presence_of :last_name, :message => "Can't be empty"
end
Run Code Online (Sandbox Code Playgroud)
我描述模型rspec和思想机器人shoulda:
describe Order do
# validations
it { should validate_presence_of(:first_name) }
it { should presence_of(:last_name) }
end
Run Code Online (Sandbox Code Playgroud)
但是我失败了:
Failures:
1) Order
Failure/Error: it { should validate_presence_of(:first_name) }
Expected errors to include "can't be blank" when first_name is set to nil, got errors: ["first_name Can't be empty (nil)", …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 4&Devise构建社交网络的课程。
我被困试图通过一门课程,这是我得到的错误:
1) Error:
UserFriendshipsControllerTest#test_: #new when not logged in should get redirected to the login page. :
NoMethodError: undefined method `authenticate!' for nil:NilClass
test/controllers/user_friendships_controller_test.rb:8:in `block (3 levels) in <class:UserFriendshipsControllerTest>'
Run Code Online (Sandbox Code Playgroud)
这是我的users_friendships_controller_test.rb:
require 'test_helper'
class UserFriendshipsControllerTest < ActionController::TestCase
context "#new" do
context "when not logged in" do
should "get redirected to the login page" do
get :new
assert_response :redirect
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的user_friendship.rb:
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: "User", foreign_key: "friend_id"
private
def friendship_params
params.require(:user).permit(:user_id, …Run Code Online (Sandbox Code Playgroud) 我正在使用shoulda和factory_girl开发REST测试.代码如下
context "on :delete to :destroy" do
setup do
@controller = NewsArticlesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@news_article = Factory.create(:news_article)
end
should "destroy new NewsArticle" do
assert_difference('NewsArticle.count', -1) do
delete :destroy, :id => @news_article.id
end
end
should_redirect_to news_articles_path
end
Run Code Online (Sandbox Code Playgroud)
结果我看到了
1) Error:
test: on :delete to :destroy should redirect to index. (NewsArticlesControllerTest):
ArgumentError: block not supplied
c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:201:in `instance_eval'
c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:201:in `__bind_1248853182_16800
0'
c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: on :delete to :destroy should redirect to index. '
Run Code Online (Sandbox Code Playgroud)
你能告诉我PLZ - 什么是错的以及我如何修改测试以使它们正常工作?
UPD:路线看起来很好
news_articles …Run Code Online (Sandbox Code Playgroud) 模型
validates_length_of :description, :maximum => 255, :allow_nil => true
Run Code Online (Sandbox Code Playgroud)
spec_file
it { should ensure_length_of(:description).is_at_most(255).allow_nil }
Run Code Online (Sandbox Code Playgroud)
回归
Failure/Error: it { should ensure_length_of(:description).is_at_most(255).allow_nil }
NoMethodError:
undefined method `allow_nil' for #<Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher:0x0000000acb03e0>
Run Code Online (Sandbox Code Playgroud)
请帮忙!
我正在使用Ruby on Rails 4.2.0和rspec-rails 3.1.0以及应用匹配器2.7.0
当运行测试我有这个错误
Failure/Error: it { should validates_presence_of :name }
NoMethodError:
undefined method `validates_presence_of' for #<RSpec::ExampleGroups::Profile::Vaidations:0x00000007881768>
# ./spec/models/profile_spec.rb:5:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
这是我的模特
class Profile < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
validates :user, presence: true
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
validates :name, presence: true, length: {maximum: 50}
validates :phone, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
validates :age, presence: true, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用shoulda匹配器进行一些基本的rspec测试,并且我遇到了一个错误,我之前没有看过这个问题.
我有一个名为name的唯一属性,但由于项目所需的原因,我在config/locales/en.yml中覆盖了我自己的消息形式的默认"已经被拍摄"消息,而且似乎不喜欢它.
我收到此错误消息
Failure/Error: it { should validate_uniqueness_of(:name) }
Flavor did not properly validate that :name is case-sensitively unique.
Given an existing Flavor whose :name is ‹"Factory Flavor Namea"›,
after making a new Flavor and setting its :name to ‹"Factory Flavor
Namea"› as well, the matcher expected the new Flavor to be invalid and
to produce the validation error "has already been taken" on :name. The
record was indeed invalid, but it produced these validation errors
instead:
* name: ["This …Run Code Online (Sandbox Code Playgroud) 使用shoulda和FactoryGirl测试模型验证。
工厂
FactoryGirl.define do
factory :tag do
value { Faker::Lorem.word }
user
end
end
Run Code Online (Sandbox Code Playgroud)
标签模型
class Tag < ApplicationRecord
validates :value,
presence: true,
uniqueness: { case_sensitive: false }
belongs_to :user
has_and_belongs_to_many :cards
end
Run Code Online (Sandbox Code Playgroud)
标签规格
RSpec.describe Tag, type: :model do
describe 'validations' do
it { should validate_presence_of(:value) }
it { should validate_uniqueness_of(:value) }
end
describe 'associations' do
it { should belong_to(:user) }
# it { should have_and_belong_to_many(:cards) }
end
end
Run Code Online (Sandbox Code Playgroud)
运行测试时出现以下错误,
Failures:
1) Tag validations should validate …Run Code Online (Sandbox Code Playgroud) shoulda ×10
rspec ×5
activemodel ×1
devise ×1
factory-bot ×1
minitest ×1
mongoid ×1
rspec-rails ×1
rspec2 ×1
ruby ×1
tdd ×1
unit-testing ×1