Tag: shoulda

应该是rspec匹配器:on =>:create

我正在使用一些Shoulda rspec匹配器来测试我的模型,其中一个是: describe Issue do it { should_not allow_value(“test”).for(:priority) } end 我的问题是我的模型中的validation如下所示: validates_format_of :priority, :with => /^(Low|Normal|High|Urgent)$/, :on => :update 因此,在运行此测试时,我得到: 1) ‘Issue should not allow priority to be set to “test”‘ FAILED Expected errors when priority is set to “test”, got errors: category is invalid (nil)title can’t be blank (nil)profile_id can’t be blank (nil) […]

如何使用rspec和factory测试模型的方法

我是铁杆上的新手,我必须用’Rspec’,’shoulda’和’factory girl’gem为现有的rails应用程序编写测试。 我可以测试非特定的测试,例如validates_presence_of:与’sholda’匹配器的东西。 但我想测试模型中的方法。 我可以想象我需要做什么,但我不能写作。 这就是我所说的一个例子: . . . context ‘is editable if project is not started’ do setup do @brief=Factory(:brief) @started_project=Factory(:project_started, :brief => @brief) @brief_have_no_project=Factory(:brief) end specify “with editable brief” do @brief.brand_info = ‘bla bla bla’ #change brand info, this is impossible #i can’t compose this section :S end specify “with non-editable brief” do […]

shoulda匹配器应该validate_uniqueness_of与范围失败

我有3个型号,用户,游戏和播放器。 用户和游戏之间基本上存在多对多的关系,玩家作为联接表,除了玩家有其他信息,因此它有自己的模型。 玩家需要游戏ID和用户ID的唯一组合,所以我试着在玩家中说: validates_uniqueness_of :user_id, :scope => :game_id 然后在我的规范中,我说(使用shoulda matchers): it { should validate_uniqueness_of(:user_id).scoped_to(:game_id)} 以下是玩家定义的关系: belongs_to :game, :inverse_of => :players belongs_to :user, :inverse_of => :players 但我在该规范上得到一个ActiveRecord :: statementinvalid错误 ActiveRecord::StatementInvalid: Mysql2::Error: Column ‘game_id’ cannot be null: INSERT INTO `players` ETC… 知道出了什么问题吗?

Rspec validates_uniqueness_of测试失败并出现其他validation错误

我在答案模型上有2个范围的唯一性validation,我试图使用Rspec和相应的shoulda匹配器来测试这些。 如下面的test trace所示,唯一性validation消息存在于errors数组中,但是,还存在2个,有时还存在3个其他错误; “body can’t be blank (nil)”, “body is too short (minimum is 10 characters) (nil)”, “user_id can’t be blank (nil)” 。 我不确定它们来自哪里,因为body和user属性是在before块中明确设置的。 如何更正这些额外错误,以便唯一性测试通过? answer.rb validates_uniqueness_of :correct, scope: :question_id, if: :correct?, message: “You can only have 1 correct answer per question” validates_uniqueness_of :user_id, scope: :question_id, message: “Only 1 answer per question per user” /* […]

如何生成一个报告,向我展示Rails 3.2,Ruby 1.9中最慢的运行测试?

我知道RSpec有–profile选项,但我只是将MiniTest / shoulda用于我当前的项目。

使用shoulda重构Rails模型上的rspec测试

通过回答关于属性可访问性测试的另一个StackOverflow问题 (并认为它们非常棒)来了解shoulda-matchers后,我决定尝试重构我在The Rails Tutorial中所做的模型测试,试图使它们更加简洁和彻底。 我这样做归功于来自模块的文档的一些灵感: Shoulda::Matchers::ActiveRecord和Shoulda::Matchers::ActiveModel ,以及这个StackOverflow关于结构化应该在模型中进行测试的答案 。 但是,还有一些我不确定的事情,我想知道如何使这些测试更好。 我将使用Rails教程中的用户规范作为我的示例,因为它是最详细的,并涵盖了许多可以改进的领域。 以下代码示例已从原始user_spec.rb更改,并将代码替换为describe “micropost associations”行。 针对user.rb模型的规范测试及其工厂在factories.rb中定义。 规格/型号/ user_spec.rb # == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # password_digest :string(255) # remember_token :string(255) […]

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

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

Shoulda Matcher和has_many通过:未定义的方法`class_name’为nil:NilClass

我试图通过与Rspec和Shoulda匹配器的关系测试a:has_many。 # student.rb has_many :presences has_many :calls, through: :presences # student_spec.rb it { should have_many(:presences) } it { should have_many(:calls).through(:presences) } #presence.rb belongs_to :call belongs_to :student #presence_spec.rb it { should belong_to(:call) } it { should belong_to(:student) } #call.rb has_many :presences has_many :students, through: :presences #call_spec.rb it { should have_many(:presences) } it { should have_many(:students).through(:presences) } […]

Rails 4 – 未定义的方法’authenticate’为nil:NilClass

我正在开发一个使用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 ‘ 这是我的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 […]

与rspec-rails 3.0.1和shoulda的测试关联不起作用

目前,使用rspec-rails(2.14.2),我使用shoulda(3.5.0)gem测试模型规范中的关联,如下所示: # app/models/user.rb class User < ActiveRecord::Base belongs_to :school end # spec/models/user_spec.rb describe User do it { should belong_to :school } end 经过一些研究,我试图让与关联相关的断言起作用(它们似乎都失败了)。 错误信息: 1) User Failure/Error: it { is_expected.to belong_to :school } NoMethodError: undefined method `belong_to’ for # # ./spec/models/user.rb:4:in `block (2 levels) in ‘ 所以我的问题是: 你可以测试没有shoulda gem的协会吗? 根据我在“expect”语法中看到的内容,这似乎不可行。 对于每个人来说,shoulda gem是否会与rspec 3.0.1打破? 有解决方法吗?