我尝试过单元测试,我不是一个大粉丝; 它更多的是负担而不是恩惠.那么我应该对这个简单的2D点类进行哪些测试呢?
public class Point{
int x;
int y;
public Point(int px, int py) {
x = px;
y = py;
}
public double distanceTo(Point other) {
// Pythagorean theorem
}
public ArrayList<Point> lineTo(Point other) {
// Bresenham's line algorithm. The same thing I've
// implemented a dozen times in differnt languages
// and can type from memory....
}
}
Run Code Online (Sandbox Code Playgroud)
每种方法需要多少次测试?另一点可以与第一个或同一个四个象限中的一个位于同一位置,或者为空.那么每种方法有六个单元测试吗?
有可能让代码如此简单明了,以至于单元测试提供的价值很小,以至于它们不值得吗?
(到目前为止的响应是:应该为任何可以为null的参数,可以作为对象本身的任何参数(即this.distanceTo(this)),任何可能导致溢出的参数以及任何可能导致溢出的参数编写测试可能会导致浮点精度损失.至少有四件事要写测试.)
使用ActionMailer发送带附件的电子邮件时遇到问题.
当我在gmail中阅读我的消息时,我的附件有'noname'文件名.
通知功能
class Notifier < ActionMailer::Base
def send_message
attachments["text.txt"] = {:content => "hello"}
mail(:to => "me@gmail.com", :subject => 'test')
end
end
Run Code Online (Sandbox Code Playgroud)
邮件标题:
Date: Sun, 19 Dec 2010 23:18:00 +0100 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; filename=text.txt Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=text.txt Content-ID: ...
如何发送具有正确文件名的邮件?
谢谢
考虑以下关联:
class Product < ActiveRecord::Base
belongs_to :shop
accepts_nested_attributes_for :shop
end
Run Code Online (Sandbox Code Playgroud)
如果
params[:product][:shop_attributes] = {"name" => "My Shop"}
Run Code Online (Sandbox Code Playgroud)
我这样做:
@product = Product.new(params[:product])
@product.save
Run Code Online (Sandbox Code Playgroud)
@product正如预期的那样,创建一个名为"我的商店"的新商店并分配给它.
但是,我无法弄清楚当shop_attributes包含一些内容时会发生什么id,例如:
params[:product][:shop_attributes] = {"id" => "20", "name" => "My Shop"}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Couldn't find Shop with ID=20 for Product with ID=
Run Code Online (Sandbox Code Playgroud)
问题1
这意味着什么?
问题2
如果是这种情况,即id商店已知,并且商店id已经存在,我该如何创建@product这样的商店将被分配给它?
如果我this按以下方式添加两次LocationListener
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,3,this);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,3,this);
Run Code Online (Sandbox Code Playgroud)
,如果只删除this一次就足够了
manager.removeUpdates(this);
Run Code Online (Sandbox Code Playgroud) 我正在使用python shell来弄清楚print命令在python中是如何工作的.
当我输入
打印01
1
打印010
8
打印0100
64
打印030
24
这里发生了什么?它只是基数2吗?为什么第二个位置的"一个"打印为8?如果它是二进制的,它不应该是2吗?
我有一个django形式的只读字段,我有时想编辑.
我只希望拥有正确权限的合适用户编辑该字段.在大多数情况下,该字段已锁定,但管理员可以对其进行编辑.
使用init函数,我可以将字段设置为只读,但不能选择只读.我还尝试将可选参数传递给StudentForm.初学但是变得比我想象的要困难得多.
有没有正确的方法来完成这个?
models.py
class Student():
# is already assigned, but needs to be unique
# only privelidged user should change.
student_id = models.CharField(max_length=20, primary_key=True)
last_name = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
# ... other fields ...
Run Code Online (Sandbox Code Playgroud)
forms.py
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ('student_id', 'last_name', 'first_name',
# ... other fields ...
def __init__(self, *args, **kwargs):
super(StudentForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance:
self.fields['student_id'].widget.attrs['readonly'] = True
Run Code Online (Sandbox Code Playgroud)
views.py
def new_student_view(request):
form …Run Code Online (Sandbox Code Playgroud) 我目前正在阅读Larry Tenny和Zeeshan Hirani撰写的EF4食谱.在阅读这本书的过程中,我偶然发现了"可组合"这个词,并且对这个词的含义有了一般意义,但没有确切的定义.
我想知道确切的定义是什么,以及是什么使(比如说)功能"可组合"?
有关更多上下文,请查看此常见问题解答(在页面上查找单词"可组合",只有一个),这与本书中的相同上下文非常相似.
这是一个段落,我对它的含义感到困惑(来自书页397):
模型定义函数的参数不显示方向.没有'out'参数,只隐含'in'参数.原因是模型定义的函数是 可组合的,可以用作LINQ查询的一部分.这可以防止它们返回输出参数中的值.
<<运算符在php中意味着什么?
例:
$t = 5;
$foo = 1 << ($t);
echo($foo);
Run Code Online (Sandbox Code Playgroud)
echo产生:32
谢谢
我在WINDOWS上使用这个脚本
public void copyFile(File sourceDirectory, File targetFile, File targetDirectory) throws IOException{
String temp = targetFile.getAbsolutePath();
String relativeD = temp.substring(sourceDirectory.getAbsolutePath().length(), targetFile.getAbsolutePath().length());
String rootD = sourceDirectory.getName();
String fullPath = targetDirectory.getAbsolutePath() + rootD + relativeD;
File fP = new File( fullPath );
System.out.println("PATH: " + fullPath);
FileChannel inChannel = new FileInputStream(targetFile).getChannel();
FileChannel outChannel = new FileOutputStream( fP ).getChannel();
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
long position = 0;
while (position < …Run Code Online (Sandbox Code Playgroud) 我正在使用基于Eclipse(Ganymede)的软件客户端,它的工作正常.然而,一件小事会让我的世界变得完美......
在Eclipse主文件夹中,有一个ini文件.有没有办法拥有各种ini文件并选择(例如通过参数或环境变量)特定的ini文件并为其创建快捷方式?
非常感谢,
eclipse configuration environment-variables command-line-arguments