所以我想使用Rspec和Factory Girl测试一些视图,但我不知道如何正确地将工厂分配给视图.我在网上看到的所有信息都使用了存根,虽然存根很好,但我想保持我的rspec内容有些一致.
我想使用工厂作为版本模型,并在渲染页面时将其与页面关联,但我考虑的所有内容似乎都被打破了.这是一个荒谬的例子:
require 'spec_helper'
describe "catalog/editions/rationale.html.haml" do
before do
@edition = Factory.create(:edition)
assign(:editions, @edition)
render :action => 'rationale', :id => @edition
end
context "GET rationale" do
it "should not have the preamble to the United States constitution" do
rendered.should_not contain(/We the People of the United States/)
end
end
end
Run Code Online (Sandbox Code Playgroud)
在这个我尝试改变渲染:action =>'reasonale',:id => @edition来渲染,以及对渲染动作的其他类似调整.我只是不知道从哪里开始一个factory_girl帮助查看.任何帮助将不胜感激.
版本:
Rails 3.0.10
RSpec 2.7
Factory_Girl 2.2
我有模型TeacherLeader,它保留其他表的外键.我可以更新这个表,我尝试为我的控制器编写测试(通过rspec).
我的模特:
# id :integer not null, primary key
# user_id :integer
# teacher_id :integer
#
class TeacherLeader < ActiveRecord::Base
belongs_to :user
belongs_to :teacher
validates :teacher_id,
:uniqueness => { :message => "already a class-head" }
end
Run Code Online (Sandbox Code Playgroud)
我的控制器
class TeacherLeadersController < ApplicationController
def edit
@teacher_leader = TeacherLeader.find( params[:id] )
...
end
def update
@teacher_leader = TeacherLeader.find( params[:id] )
...
if ( @teacher_leader.update_attributes( params[:teacher_leader] ) )
redirect_to teachers_path
...
else
redirect_to edit_teacher_leader_path
...
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
所以,我在为PUT编写测试时遇到了问题.以下是我的PUT方法代码示例:
describe TeacherLeadersController …Run Code Online (Sandbox Code Playgroud) model-view-controller rspec model rspec-rails ruby-on-rails-3
我使用bcrypt-ruby和组合了一个用户身份验证的基本应用程序has_secure_password.结果基本上是Rails教程中应用程序的准系统版本.换句话说,我有一个RESTful用户模型以及登录和注销功能.
作为编辑用户信息的测试的一部分,我编写了一个更改密码的测试.虽然更改密码在浏览器中工作正常,但我的测试不会通过.
subject { page }
describe "successful password change"
let(:new_password) { "foobaz" }
before do
fill_in "Password", with: new_password
fill_in "Password Confirmation", with: new_password
click_button "Save changes"
end
specify { user.reload.password.should == new_password }
end
Run Code Online (Sandbox Code Playgroud)
显然,我在这里误解了一些基本细节.
简而言之:
1)为什么上面的代码无法正常工作?更改密码功能在浏览器中有效.同时,rspec继续在上面的最后一行重新加载旧密码.然后测试失败了.
2)测试密码更改的更好方法是什么?
编辑:
将初始密码设置为foobar,错误消息为:
Failure/Error: specify { user.reload.password.should == new_password }
expected: "foobaz"
got: "foobar" (using ==)
Run Code Online (Sandbox Code Playgroud)
基本上,看起来before块实际上并没有保存新密码.
作为参考,相关的控制器动作如下:
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile Updated" …Run Code Online (Sandbox Code Playgroud) rspec ruby-on-rails rspec-rails ruby-on-rails-3 railstutorial.org
一个feedback模型需要account存在.每个account都有一个唯一的:在模型中验证的uuid:validates :uuid, presence: true, uniqueness: true
我使用rspec来测试反馈模型,并且Uuid has already been taken每当我构建一个feedback对象时都会出现错误,因为反馈正在构建一个关联的account
require "spec_helper"
describe Feedback do
it "is valid with a message" do
expect(build(:feedback)).to be_valid
end
it "is invalid without a message" do
expect(build(:feedback, message: nil)).to have(1).errors_on(:message)
end
it "is invalid without an associated account" do
expect(build(:feedback, account: nil)).to have(1).errors_on(:account)
end
end
Run Code Online (Sandbox Code Playgroud)
FactoryGirl.define do
factory :feedback do
association :account
message Faker::Lorem.paragraphs
end
end
Run Code Online (Sandbox Code Playgroud)
我正在构建一个带有mongoid odm的rails 4安装应用程序.一切正常,但rspec测试不能正常工作.当我运行bundle exec rspec错误时说:
工厂未注册:cafcaf_user
我的用户模型:
module Cafcaf
class User
include Mongoid::Document
field :username, type: String
field :email, type: String
field :full_name, type: String
field :last_name, type: String
end
end
Run Code Online (Sandbox Code Playgroud)
我的spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../spec/test_app/config/environment.rb", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec …Run Code Online (Sandbox Code Playgroud) 我试图安装用于TDD和BDD测试的rspec框架,以及用于在完成更改的同时运行的连续测试的自动测试宝石.
我一直在关注所有类型的教程,其中一些在堆栈溢出,其他一些在网上,但我无法达成解决方案,在执行时
rails生成rspec:install
它抛出了一个错误'找不到生成器rspec:install'.
我在这里附上了宝石文件:[更新]
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: …Run Code Online (Sandbox Code Playgroud) 注意:
class BaseValidator; end
class DefaultValidator < BaseValidator; end
Run Code Online (Sandbox Code Playgroud)
Rspec 3.1:
describe ValidatorFactory do
context "creating DefaultValidator" do
subject { ValidatorFactory.validator('default') }
it {is_expected.to be_a(DefaultValidator)}
it {is_expected.to be_kind_of(BaseValidator) }
end
end
Run Code Online (Sandbox Code Playgroud)
打印我这个:
Failure/Error: it {is_expected.to be_a(DefaultValidator)}
expected DefaultValidator to be a kind of DefaultValidator
Failure/Error: it {is_expected.to be_kind_of(BaseValidator) }
expected DefaultValidator to be a kind of BaseValidator
Run Code Online (Sandbox Code Playgroud)
但是,这有效:
...
it {is_expected.to be(DefaultValidator)}
it {is_expected.to be < (BaseValidator) }
...
Run Code Online (Sandbox Code Playgroud)
ValidatorFactory.rb
class ValidatorFactory
def self.validator(type)
case type.downcase
when 'default'
DefaultValidator
else …Run Code Online (Sandbox Code Playgroud) 我无法确定原因,但我收到了在Rails项目中多次加载共享规范的弃用警告.以下是它们的定义方式:
#spec/support/shared/authenticated_endpoints_spec.rb
RSpec.shared_examples "an authenticated endpoint" do
it_behaves_like "an authenticated show endpoint"
it_behaves_like "an authenticated index endpoint"
end
RSpec.shared_examples "an authenticated show endpoint" do
# omitted
end
RSpec.shared_examples "an authenticated index endpoint" do
# omitted
end
Run Code Online (Sandbox Code Playgroud)
我spec_helper看起来像这样:
RSpec.configure do |config|
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
end
Run Code Online (Sandbox Code Playgroud)
这是我得到的弃权警告:
WARNING: Shared example group 'an authenticated endpoint' has been previously defined at:
/Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
...and you are now defining it at:
/Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
The new definition will overwrite the original one.
WARNING: …Run Code Online (Sandbox Code Playgroud) 我在为FactoryGirl的多态关联设置工厂时遇到问题.我的模型和工厂设置如下所示:
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
class Customer < ActiveRecord::Base
has_one :address, as: :addressable
end
factory :address do
village Faker::Address.city
upazilla Faker::Address.street_name
ward Faker::Address.street_address
district Faker::Address.state
association :addressable
end
factory :customer ,class: Customer do
recharge_token 10
date_of_birth Faker::Date.backward(100)
manager
after(:create) do |customer|
customer.address = create(:address, addressable: customer)
#create( :address, addressable: customer)
end
end
Run Code Online (Sandbox Code Playgroud)
测试套件打破以下错误消息
vendor/cache/ruby/2.2.0/gems/factory_girl-4.7.0/lib/factory_girl/linter.rb:12:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError)
* address - Factory not registered: addressable (ArgumentError)
Run Code Online (Sandbox Code Playgroud)
这正是我的问题,但不幸的是他的解决方案对我不起作用.谢谢大家的时间!
我尝试过,SecureRandom.random_number(9**6)但有时返回5,有时返回6。我希望它的长度始终为6。我也更喜欢它的格式,SecureRandom.random_number(9**6)而无需使用类似的语法,6.times.map以便在控制器测试中更容易进行打桩。