Tag: tdd

使用MiniTest测试用户模型(设计认证)

我正在尝试测试用户模型,为此我设计了身份validation。 我面临的问题是,1。在fixtures中输入’password’/’password_confirmation’字段会给我无效的列’password’/’password_confirmation’错误。 如果我从fixture中删除这些列并添加user_test.rb require ‘test_helper’ class UserTest < ActiveSupport::TestCase def setup @user = User.new(name: "example user", email: "example@example.com", password: "Test123", work_number: '1234567890', cell_number: '1234567890') end test "should be valid" do assert @user.valid? end test "name should be present" do @user.name = "Example Name " assert @user.valid? end end 我得到的错误是: test_0001_should be valid FAIL (0.74s) Minitest::Assertion: […]

Rails:unit testingbefore_create?

我试图测试一个字段是通过回调正确生成的,但我无法弄清楚这个。 album.rb before_create :generate_permalink private def generate_permalink @title = album.downcase.gsub(/\W/, ‘_’) @artist = artist.downcase.gsub(/\W/, ‘_’) self.permalink = @artist + “-” + @title end album_test.rb test “should return a proper permalink” do album = Album.new(:artist=>’Dead Weathers’, :album=>’Primary Colours’) album.save assert_equal “dead_weathers-primary_colours”, album.permalink end 但这不起作用,因为如果保存, album.permalink将不会返回该值。 有没有办法测试before_create ? 我应该在控制器级别这样做吗?

使用RSpec测试密码长度validation

我正在编写一些unit testing,以确保用户模型的密码长度不超过8个字符。 我开始使用User模型: class User { :minimum => 90, :too_short => “password is too short, must be at least %{count} characters” }, :on => :create end 和user_spec.rb测试: describe User do subject { FactoryGirl.build :user } its(:password) { should have_at_least(8).items } end 但是我意识到这实际上并没有测试我的validation,只是测试我的工厂有一个> = 8个字符的密码。 除了测试有效之外,还有一种很好的方法吗? 0-7字符密码的方法? 我的理论是,如果我只测试7个字符,并且有人意外硬编码4个字符密码就可以了,这将通过validation,但实际上不是预期的。 可能有一些代码,其中取决于密码超过8个字符(不太可能,但在其他情况下可能是真的),因此允许密码4是不正确的。 在这种情况下,在模型中更改密码validation的人将不知道他们做错了什么。 我只想知道如何使用TDD正确测试这样的情况。

Ruby on Rails使用Devise身份validation进行function测试

我正在寻找一个解决奇怪问题的方法。 我有一个控制器,需要身份validation(使用设计gem)。 我添加了Devise TestHelpers,但我无法让它工作。 require ‘test_helper’ class KeysControllerTest ‘testuser@demomailtest.com’, :password => ‘MyTestingPassword’, :password_confirmation => ‘MyTestingPassword’ ) sign_in @user @key = keys(:one) end test “should get index” do get :index assert_response :success assert_not_nil assigns(:keys) end test “should get new” do get :new assert_response :success end test “should create key” do assert_difference(‘Key.count’) do post :create, :key => […]

如何将gem’minitest’添加到我的测试帮助器中?

我是Ruby on Rails和测试的新手。 当我运行rake test我收到以下错误: /Users/jarvis/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/rack-1.3.4/lib/rack/backports /uri/common_192.rb:53: warning: already initialized constant WFKV_ /Users/jarvis/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/turn-0.8.3/lib/turn/autorun /minitest.rb:14:in `’: MiniTest v1.6.0 is out of date. (RuntimeError) `gem install minitest` and add `gem ‘minitest’ to you test helper. from /Users/jarvis/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.1.1/lib /active_support/dependencies.rb:240:in `require’ 我做了gem install minitest ,现在它说要将gem’minitest’添加到我的测试帮助器中。 这是test/test_helper.rb ,如果是这样,我如何“添加”minitest?

应该匹配的ActiveRecord匹配器是否违反了“测试行为不实施”规则?

例如,如果我在我的规范中使用should validate_presence_of ,那只是测试我在模型中有validate_presence_of代码片段,那就是测试实现。 更重要的是,测试真正的问题并不是那个规范完全没用,“如果我没有填写某个字段,那么模型是否会成功保存?”

railstutorial.org,第6章。未知属性:密码

我已经完成了railstutorial的第6章,但是在我添加了password和password_confirmation以及以下错误后,我的所有用户模型规范都开始失败: Failures: 1) User Failure/Error: @user = User.new(name: “Example User”, email: “user@example.com”, ActiveRecord::UnknownAttributeError: unknown attribute: password # ./spec/models/user_spec.rb:17:in `new’ # ./spec/models/user_spec.rb:17:in `block (2 levels) in ‘ 2) User Failure/Error: @user = User.new(name: “Example User”, email: “user@example.com”, ActiveRecord::UnknownAttributeError: unknown attribute: password # ./spec/models/user_spec.rb:17:in `new’ # ./spec/models/user_spec.rb:17:in `block (2 levels) in ‘ 3) User Failure/Error: @user = […]

Rails灯具 – 你如何设置外键?

我正在阅读本指南中关于Rails装置的内容(谢谢,trevorturk)。 看来你在Yaml文件中定义了类,它们会自动加载到test数据库中 – 很酷。 但是如果你想指定这个食谱属于那个食谱(或其他什么),你怎么做? 您是否应该在Yaml代码中手动指定cookbook.id和recipe.cookbook_id的值? (只是一个猜测 – 指南没有显示那样的东西。)或者是否有更合适的方式?

显示每个rspec示例的运行时

目前我正在运行超过1k的例子,这需要很长时间才能完成(超过20分钟!!! )。 我想确定哪些示例需要花费更多时间才能完成,有没有办法运行rspec并返回每个示例完成(单独)所需的时间? 我正在使用rspec 1.3.0和rspec-rails 1.2.3

Rails Rspec错误 – 未定义的方法`访问’

所以我是TDD的新手,我在我的测试中抛出一些Rspec错误…基本上在运行bundle exec rspec spec之后,我得到一些undefined method ‘visit’错误我的一些规范。 任何有关如何通过这些测试的帮助将非常感激:谢谢。 Failures: 1) User pages profile page Failure/Error: before { visit user_path(user) } NoMethodError: undefined method `visit’ for # # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in ‘ 2) User pages profile page Failure/Error: before { visit user_path(user) } NoMethodError: undefined method `visit’ for # # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in […]