我碰壁了,因为我对这个 rspec 错误消息的含义感到非常困惑。我正在测试一件艺术品是否有成本。以下是我的 rspec 片段:
let(:valid_art_piece){ { date_of_creation: DateTime.parse('2012-3-13'),
placement_date_of_sale: DateTime.parse('2014-8-13'), cost: 250, medium: 'sculpture',
availability: true } }
it 'requires a cost' do
art_piece = ArtPiece.new(valid_art_piece.merge(cost: ''))
expect(art_piece).to_not be_valid
expect(art_piece.errors[:cost].to include "can't be blank")
end
Run Code Online (Sandbox Code Playgroud)
错误信息:
1) ArtPiece requires a cost
Failure/Error: expect(art_piece.errors[:cost].to include "can't be blank")
NoMethodError:
undefined method `+' for #<RSpec::Matchers::BuiltIn::Include:0x000001050f4cd0>
# ./spec/models/artist_piece_spec.rb:30:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
就我而言,这不应该失败,我不知道为什么会失败。我的 schema.rb 将该字段设为 null false,我的模型使用 numericity: true 选项对其进行验证。
class ArtPiece < ActiveRecord::Base
validates_presence_of :date_of_creation
validates_presence_of :placement_date_of_sale
validates :cost, …Run Code Online (Sandbox Code Playgroud) 我正在阅读Sandi Metz的POODR,并且遇到了一个我不太了解的编码原理.这是代码:
class Bicycle
attr_reader :size, :chain, :tire_size
def initialize(args = {})
@size = args[:size] || 1
@chain = args[:chain] || 2
@tire_size = args[:tire_size] || 3
post_initialize(args)
end
end
class MountainBike < Bicycle
attr_reader :front_shock, :rear_shock
def post_initialize(args)
@front_shock = args[:front_shock]
@rear_shock = args[:rear_shock]
end
end
mb = MountainBike.new(front_shock: 4, rear_shock: 5)
puts mb.size
puts mb.chain
puts mb.tire_size
puts mb.front_shock
puts mb.rear_shock
Run Code Online (Sandbox Code Playgroud)
此代码将输出1,2,3,4,5其各自的属性.我不明白的是方法查找.
当山地自行车被实例化时,因为它没有自己的initialize方法,它将沿着方法查找链向上移动到它的超类(Bicycle)上.但现在从那里开始,似乎Bicycle再次回到MountainBike的post_initialize方法.它不是继续推进方法链,而是如何回归?是post_initialize一个ruby关键字initialize,因为它提供某种特殊功能?是否有一些其他红宝石内省方法可以用来查看发生了什么?
我有以下搜索词:
"login:17639 email:fakemail@gmail.com ref:co-10000 common_name:testingdomain organization:'Internet Company'"
该术语源自 params 变量,其中左侧的:所有内容都是过滤器术语,右侧的所有内容都是过滤器:的值。我想要做的是将术语拆分为键和值,并从它们创建一个散列。这是最终目标:
search_filters = {
login:17639,
email:'fakemail@gmail.com',
etc, etc,
}
Run Code Online (Sandbox Code Playgroud)
我正在split, gsub, tr尝试获得这些值,但我在组织领域遇到了问题。这是我到目前为止所拥有的:
term.gsub(/'/,'').tr(':', ' ').split(" ")
term.gsub(":")
Run Code Online (Sandbox Code Playgroud)
基本上,还有许多其他变体,如上述。问题是组织领域。每次迭代都会产生这样["organization", "Internet", "Company"]的结果,问题是“互联网公司”正在分裂。我不能为这个过滤器放置一个简单的 if/else 语句来将它们粘合在一起,因为有更多的过滤器需要处理。有没有办法可以更容易地根据冒号来划分过滤项?谢谢你。
我正在做一个Ruby kata,要求我找到从1到N(包括两端)的所有数字的数字之和.
所以如果我有这些输入,我会得到这些输出:
For N = 10 the sum is 1+2+3+4+5+6+7+8+9+(1+0) = 46
For N = 11 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) = 48
For N = 12 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) +(1+2)= 51
Run Code Online (Sandbox Code Playgroud)
现在我知道需要做些什么.以下是我必须解决此问题的代码:
def solution(n)
if n <= 9
return n if n == 1
solution(n-1) + n
elsif n >= 10
45 + (10..n) #How can I grab the ones,tenths, and hundreds?
end
end
Run Code Online (Sandbox Code Playgroud)
基本上一切都很好,直到我超过10.
我试图找到某种可以做到这一点的方法.我搜索了Fixnum和Integer,但我找不到任何可以帮助我的东西.我想要的是找到类似"string"[0]但当然不必在字符串和整数之间返回整数的东西.我知道那里有数学关系,但我很难解读.
任何帮助,将不胜感激.
我有以下测试:
it 'create action: a user replies to a post for the first time' do
login_as user
# ActionMailer goes up by two because a user who created a topic has an accompanying post.
# One email for post creation, another for post replies.
assert_difference(['Post.count', 'ActionMailer::Base.deliveries.size'], 2) do
post :create, topic_id: topic.id, post: { body: 'Creating a post for the first time.' }
end
email = ActionMailer::Base.deliveries
email.first.to.must_equal [user.email]
email.subject.must_equal 'Post successfully created'
must_redirect_to topic_path(topic.id)
email.last.to.must_equal [user.email]
email.subject.must_equal 'Post reply …Run Code Online (Sandbox Code Playgroud) 我试图在我的 Flask 应用程序中声明一个 User 模型,以便使用 Flask-Login 扩展实现登录。从有关 sql alchemy 的烧瓶文档中,有一个示例,我已将其用于另一个名为“员工”的模型。这是代码:
class Employee(db.Model):
__tablename__ = "employees"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
title = db.Column(db.String(200))
email = db.Column(db.String(200))
department = db.Column(db.String(200))
def __init__(self, name, title, email, department):
self.name = name
self.title = title
self.email = email
self.department = department
def __repr__(self):
return '<Employee %r>' % self.name
Run Code Online (Sandbox Code Playgroud)
这是取自此页面作为示例Flask SQL-Alchemy Docs
我很困惑,因为我也在使用 alembic 来运行迁移,所以通过使用alembic revision -m "create user table我已经创建了一个“用户”表。我使用 Flask Sql-Alchemy 指南的推荐创建了我的第一个模型表(员工):
from yourapplication.database import init_db
init_db()
Run Code Online (Sandbox Code Playgroud)
这是混乱。我现在需要建立一个用户模型进行身份验证。我该怎么做呢?到目前为止,这是我的代码:
class …Run Code Online (Sandbox Code Playgroud) 是否有某种方法或资源我可以参考,这将允许我将我的整个应用程序导入python解释器?
例如,当我运行python一些flask-sql查询时,我必须这样做每次我退出:
python
import project
from project import app,db, etc etc
from project.models import Model, Model,
goes on and on......
Run Code Online (Sandbox Code Playgroud)
我怎样才能避免这样做以绕过重复性呢?来自Rails,它非常适合运行rails c并为您装载所有东西.
我正在解决上述标题中的错误。我明白发生了什么,但在尝试解决此错误时,我遇到了一个有趣的 Github 问题及其解决方案。但是,我不明白解决方案。
基本上,一位用户报告说解决此问题的方法是实施以下操作:
只需确保 Capistrano 将尝试符号链接到的任何目录不作为物理目录存在。这可能也适用于您添加到 :linked_files 或 :linked_dirs 配置选项中的任何文件或目录。
我的问题很简单。当该用户说确保该目录不是物理目录时,他是什么意思?
我正在尝试将 rspec 和 shoulda 匹配器作为我的新测试框架,但我对如何通过should validate_uniqueness_of(:attribute)测试感到有些困惑。这是我的简单模型:
class Employee < ActiveRecord::Base
validates :name, presence: true
validates :title, presence: true
validates :department, presence: true
validates :avatar, presence: true
validates :email, presence: true, uniqueness: true
validates :reports_to, presence: true
mount_uploader :avatar, EmployeeAvatarUploader, on: :file_name
end
Run Code Online (Sandbox Code Playgroud)
这是初始测试:
RSpec.describe Employee, type: :model do
it { should validate_presence_of(:name) }
it { should validate_presence_of(:title) }
it { should validate_presence_of(:department) }
it { should validate_presence_of(:avatar) }
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email) } …Run Code Online (Sandbox Code Playgroud) 我试图mobx用我的React 360制作一个vr应用程序。我尝试使用装饰器语法,但是在浪费大量时间尝试实现它之后,我决定使用nondecorator语法。这是我遇到问题的mobx文档中遇到的一个示例。这是代码:
import {observer} from "mobx-react";
var timerData = observable({
secondsPassed: 0
});
setInterval(() => {
timerData.secondsPassed++;
}, 1000);
@observer class Timer extends React.Component {
render() {
return (<span>Seconds passed: { this.props.timerData.secondsPassed } </span> )
}
};
ReactDOM.render(<Timer timerData={timerData} />, document.body);
Run Code Online (Sandbox Code Playgroud)
注意类observer上的声明Timer。文档指出了这一点。
请注意,使用@observer作为装饰器是可选的,observer(class Timer ... {})实现的效果完全相同。
这是实现的正确方法Timer吗?
observer(class Timer extends React.Component {
render() {
return (<span>Seconds passed: { this.props.timerData.secondsPassed } </span> )
}
})
Run Code Online (Sandbox Code Playgroud) ruby ×5
flask ×2
python ×2
rspec ×2
capistrano ×1
flask-login ×1
javascript ×1
mobx ×1
mobx-react ×1
reactjs ×1
regex ×1